I read Why is the range of bytes -128 to 127 in Java? it says
128 is 10000000. Inverted, it's 01111111, and adding one gets 10000000 again
so it concludes -128 is 10000000
so +128 cannot be represented in 2's complement in 8 bits, but that means we can represent it in 9 bits, so 128 is 010000000 and so taking its 2's complement -128 is 110000000,
so is representation of -128 10000000 or 110000000 ? Is the representaion bit dependent ?
Why not simply make the lower range -127 fot 8 bits instead of writing -128 as 10000000 ?
The range of 2's complement form is from (2(n-1)) to (2(n-1)-1). For example, range of 6 bit 2's complement form binary number is from (25) to (25-1) which is equal from minimum value -32 (i.e., 1 00000) to maximum value +31 (i.e., 0 11111).
The valid range for an 8-bit signed is -128 to 127. If you have values larger than 127 or less than -128 in the input, then you will need to use a 16-bit signed.
The byte is a unit of digital information that most commonly consists of eight bits.
Why is the range of unsigned byte is from -128 to 127?
It's not. An unsigned byte (assuming 8-bit) is from 0 to 255.
The range of a signed byte using 2's complement is from -128 to 127, directly from the definition of 2's complement:
01111111 = +127 01111110 = +126 01111101 = +125 ... 00000001 = +1 00000000 = 0 11111111 = -1 ... 10000010 = -126 10000001 = -127 10000000 = -128
so is representation of -128 10000000 or 110000000 ?
In 8-bit, it's 10000000
, in a hypothetical 9-bit representation it's 110000000
.
Why not simply make the lower range -127 for 8 bits?
Artificially restricting the range to -127 wouldn't achieve very much; you'd be disallowing a perfectly valid value, and generally making code more complex (what else would you do with the bit pattern 10000000
?).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With