2
$\begingroup$

I have this confusion regarding binary representation of decimal value 129 (or even 128). If 8 bits are used to represent numbers when doing the two's complement, then we know that '00000000' to '01111111' are used for 0 to 127 and leading 1 is used for negative numbers i.e. 1xxxxxxx where x is any combination of 0's and 1's to represent the value of that negative number. But then how is 128 represented in 8 bits because the first bit is reserved for telling the number is negative (basically a signed number)?

$\endgroup$
1
$\begingroup$

The range of integers representable in 8 bits using two's complement is –128 to +127.

If instead the numbers are unsigned, then the range becomes 0 to 255.

$\endgroup$
  • $\begingroup$ So does this mean that 128 is 10000000 when we are using 8 bits and it will not be confused for a negative number? Then how can we tell if we are looking at a negative 8-bit number or an unsigned 8-bit number (in cases where the first bit is 1)? $\endgroup$ – stackuser Aug 3 at 20:51
  • $\begingroup$ 128 is not between -128 and +127. As for how to tell whether a number is signed or unsigned, it’s impossible. It’s up to the programmer to decide whether she interprets a byte as a signed 8-bit integer, an unsigned 8-bit integer, or anything else. $\endgroup$ – Yuval Filmus Aug 3 at 22:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.