This week's book giveaway is in the Agile and other Processes forum.We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!See this thread for details.
Big Moose Saloon
 Search | Java FAQ | Recent Topics Register / Login Win a copy of The Mikado Method this week in the Agile and other Processes forum!

# byte size and range

Andy Jenks
Greenhorn

Joined: Feb 13, 2002
Posts: 3
This is confusing me so any help explaining this would be helpful.
1)
a byte has 8 bits of storage. The MSB is the left bit and 1 indicates negative and 0 indicates positive.
2)
byte range is -128 to 127.
Since there are only 7 bits of storage(as 1 bit is used for the sign) how does it store -128?
e.g., 01111111 is 127
and 11111111 is -127
in binary 128 is 10000000 (so how can a byte hold -128?
Andy
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
11111111 = -1 actually.
01111111 = 127
10000000 = -128

Rob
SCJP 1.4
Brian Lugo
Ranch Hand

Joined: Nov 10, 2000
Posts: 165
In a byte if you don't have a sign bit then,
128 = 10000000
So lets see how to get -128 from this:
1. First invert/complement the bits: 01111111
2. Then add 1 to it: 10000000
So, this represents -128 in two's complement form.
With the first bit indicating the sign bit.
Until and unless I have forgotten the principles of digital logic that I learnt 12 years ago ...
Andy Jenks
Greenhorn

Joined: Feb 13, 2002
Posts: 3
Thanks robs its Twos complement then right
ie: I got this for how to calculate negative binaries
Thanks
Andy

How to find the Twos Complement of a number: A Quick Method
Suppose we want to find the twos complement 6-bit representation of -14
(1.) First, we write down the positive binary number, in this case 14
14 - 001110
(2.) Now, start writing the digits from the right hand side up to and including
the first 1. In our case, just 10.
(3.) Then invert the rest of the digits. So our answer becomes 110010. That's all!
Jennifer Wallace
Ranch Hand

Joined: Nov 30, 2001
Posts: 102
Two's complement representation of some bits to clarify! To get a negative number representaion,invert the bits of the corresponding number and add 1.
0 = 0000 0000
1 = 0000 0001 <---> -1 = 1111 1111
2 = 0000 0010 <---> -2 = 1111 1110
3 = 0000 0011 <---> -3 = 1111 1101
4 = 0000 0100 <---> -4 = 1111 1100
.
.
.
127 = 0111 1111 <---> -127 = 1000 0001
128 = 1000 0000 <---> -128 = 1000 0000
So, it all fits into 8 bits!
[ February 22, 2002: Message edited by: Jennifer Wallace ]

I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.

subject: byte size and range