Electrical – If a 4-bit binary digit in 2’s Complement form is stored in 8 bits how is it represented

binarydigital-logic

I am quite new to Digital Logic. I got this doubt while solving a problem.

If a 4 bit binary digit in 2's complement e.g. X3 X2 X1 X0 is stored in an 8 bit space what will be the representation in those 8 bits.

As far as i know, The MSB in 2's complement form is the sign bit.

So if e.g X3 X2 X1 X0 = 0 0 0 0 then X7 X6 X5 X4 X3 X2 X1 X0 = 0 0 0 0 0 0 0 0

if e.g X3 X2 X1 X0 = 1 1 0 1 then X7 X6 X5 X4 X3 X2 X1 X0 = 1 1 1 1 1 1 0 1

Could you generalize the way in which we will represent 4 bits in 2's complement to 8 bits?

Best Answer

In 2's complement form, a positive number stored in 8 bits is represented as 0x00 to 0x7F (0 to 127 in decimal).

Negative numbers range from 0xFF to 0x80 (-1 to -128 in decimal).

For example -1 is the 1's complement of 0x01 = 0xFE (8 bits) , with 0x01 added = 0xFF.

You obviously need 5 bits to represent a signed decimal digit (there are 19 possibilities), and effectively the 5th bit (bit 4 as we usually count it) gets copied into bits 5, 6 and 7. So -9 is the complement of 0x09 = 0xF6, plus 1 = 0xF7.

Similarly, if you were representing the single signed digit in a 32-bit wide variable, 1 would be 0x00000001 and -1 would be 0xFFFFFFFF (and -9 would be 0xFFFFFFF7).