Questions tagged [twos-complement]

Two's complement is a mathematical operation on binary numbers, as well as a binary signed number representation based on this operation.

A two's complement number follows the same principal as a pure binary number, except for the first bit: the most significant bit. If this bit is 0, the binary number is positive, if it's 1 then the number will be negative.

For example:

01110 = 8 + 4 + 2
      = 14

10010 = -16 + 2
      = -14

So 01110 is 14 in denary and 10010 is -14 in denary

683 questions
2
votes
0 answers

Assembly movsbq instruction

I understood the difference between movz and movs. movz fills the empty bytes with zeros while movs fills them with sign bit (zeros or ones). However, I came accross the following example movabsq $0x0011223344556677, %rax movb $0xAA, %dl movb %dl,…
user14596364
2
votes
1 answer

Why is MSB used as the sign bit?

I was reading Kip Irvine's book on x86 assembly programming when in the explanation of signed integers, it was mentioned that the MSB (Most Significant Bit) is used as the signed bit, 0 for +ve and 1 for -ve, if I am not wrong. However, I cannot…
2
votes
2 answers

What are widening integer operations?

Let's examine Add and Multiply as examples. Which one can be categorized as widening? Assume inputs are signed characters (i.e 8 bits in length) in 2's complement, unless declared otherwise. Positive number addition 1+2 = 3 This operation doesn't…
Dan
  • 2,694
  • 1
  • 6
  • 19
2
votes
1 answer

What is the advantage of this sizing code in C?

Apologies for the generic question title, I wasn't sure how to phrase it properly (suggestions welcome!) I'm trying to get my head around some of the code for the Common Mark parser and came across this: /* Oversize the buffer by 50% to guarantee…
Henry Cooper
  • 97
  • 1
  • 2
  • 10
2
votes
1 answer

Signed Multiplication of 1024-bit 2's complement numbers using 32-bit chunks

So I have the following struct definition for my 1024-bit number (I want to use 2's complement representation here and I am on a 32-bit system): typedef struct int1024 { int32_t num[32]; //should I use uint32_t? } int1024; Basically an array…
yosmo78
  • 489
  • 4
  • 13
2
votes
2 answers

Why does 2's complement sign extension work by adding copies of the sign bit?

Let's take the example of sign-extending a 16-bit signed number into a 32-bit register, such as mov $+/-5, %ax movswl %ax, %ebx. There are two possible cases: High bit is zero (number is positive). This is very easy to understand and intuitive. For…
2
votes
5 answers

C - where is the signed bit in the negative char—(-128)—when it's always encoded as the 2's complement of its binary?

I understand that the most significant bit in a signed integer is used to encode the sign. I also get that a negative integer is almost always encoded as the 2's complement of its binary. I am having trouble reconciling the two facts mentioned…
computronium
  • 445
  • 2
  • 11
2
votes
3 answers

Binary 2's complement multiplication by hand?

I have to solve a MIPS multiplication by hand and I am having trouble. I have two registers, $8 which holds a two's complement representation of -1073741824 (which is 2^30) and $9 with two's complement of +3, I need to find the result of this MIPS…
drainojunkie
  • 65
  • 1
  • 1
  • 6
2
votes
1 answer

Converting hexadecimal number to its 2's complement

I'm supposed to write a program in C that switches hex number to its 2's complement. e.g) 0000008f to ffffff71 so far I have figured out how to print out 2'c complement of binary, but hex seems like a different story to me. Any suggestions about…
MerZer0
  • 25
  • 1
  • 1
  • 5
2
votes
2 answers

Negative fixed point number representation

I am writing a generic routine for converting fixed-point numbers between decimal and binary representations. For positive numbers the processing is simple, however when things come to negative ones I found divergent sources. Someone says there is a…
Eddie Deng
  • 1,399
  • 1
  • 18
  • 30
2
votes
4 answers

Bitwise Complement

I have a sample problem with w=1, y=7, z=0, x = ~(w && y) | y; and the solution is x = -1, but I can't figure out why? Here is my thought process: (w && y) = (1 && 7) = 1 ~1 1 in bits is 0000 0001 ~1 in bits is 1111 1110 Not sure what to do from…
raphnguyen
  • 3,565
  • 18
  • 56
  • 74
2
votes
5 answers

Convert signed int of variable bit size

I have a number of bits (the number of bits can change) in an unsigned int (uint32_t). For example (12 bits in the example): uint32_t a = 0xF9C; The bits represent a signed int of that length. In this case the number in decimal should be -100. I…
oliverleo
  • 77
  • 9
2
votes
2 answers

Normalizing a two complement number

I am working on some Tensilica processor and I don't understand the normalization process. NSA - Normalized Shift Amount Usage:     NSA at, as NSA calculates the left shift amount that will normalize the twos complement contents of address register…
yo3hcv
  • 1,531
  • 2
  • 17
  • 27
2
votes
1 answer

Minimum value can't be saved in byte

According to https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html a byte in Java is: "The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). " If I…
2
votes
1 answer

How to find the absolute value of an integer in Two's complement in ARM

If I have -2 (11111111111111111111111111111110), is there a neat ARM instruction or a series of such that will make it (00000000000000000000000000000010). OR or XOR would not work from what I have tried since I loose the 30th bit. Thank you
boinka
  • 145
  • 1
  • 11