Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to represent out of range number in binary?

Tags:

binary

In 2 complements I read from wikipedia, the range is from -128 to 127. So I wonder how do we represent 128 in 2 complement as it is out of range above?


2 Answers

You use more bits.

The range -128 to +127 is 256 unique values, which is 8 bits. If you need a larger range, you need more bits.

There is nothing restricting 2s-complement numbers to 8-bit values. For instance, a 16-bit 2s-complement number ranges from -32768 to +32767.

like image 183
Mark Rushakoff Avatar answered Nov 20 '25 17:11

Mark Rushakoff


8-bit gives you the range: [-2^7 , 2^7-1] = [-128,127]

In general, 2's complement using n-bit can represent numbers in the range:

-2^(n-1) to +2^(n-1)-1
like image 39
merv Avatar answered Nov 20 '25 16:11

merv



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!