This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on byte

 
sanjana narayanan
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain to me the following..
int i= 256;
byte b = (byte)i;
System.out.println("byte value"+b);
prints 0 // reason it goes beyond the range of byte(-128 to 127)
but
int i=130
byte b = (byte)i;
System.out.println("byte value"+b);
prints 126 // how does this happen?
sanjana
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sanjana,
The output is: byte value-126
b is �126.
int i = 130; //0000 0000 0000 0000 0000 0000 1000 0010
byte b = (byte)i; //1000 0010

-x == ~x + 1
~x == -x - 1
[ December 02, 2003: Message edited by: Marlene Miller ]
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sanjana,
The output is -126 and it is NOT 126. MM explained it clearly, however i want to add some notes. If you encounter such a question in the exam, the easiest way to solve it is by this:
|--------|-------|
-128 <-- 0 --> 127
start counting from zero, when you reach to the end (i.e 127) then continue count from -128.
in your example i = 130
start counting until you reach 127 then continue -128, -127 and finally -126 (Which is your answer).
hope this helps.
[ December 03, 2003: Message edited by: Vicken Karaoghlanian ]
 
CH Lee
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vicken,
Wow!!! That was indeed a very great tips from you!!! I will put in into my Palm.
Can I say the reason for i = 256 will print 0 is because it 'recycle' back to 0?
CH
 
sanjana narayanan
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank u guys..
Yes u'r right. The ans is -126 and 126.
sanjana
 
sanjana narayanan
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops..It's -126 and NOT 126
Sanjana
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd like to suggest another way to think about 130.
A byte has 8 bits. 8 bits can hold 256 unsigned numbers, 0 to 255. The first half of those 256 numbers are 0 to 127. The second half 128 to 255 are designated as the negative numbers of the first half.
This is the most important fact of two's complement arithmetic:
x + (-x) == 2^8 == 256
130 belongs to the second half. It represents the negative of some number in the first half.
126 + 130 == 256
130 represents -126.
[ December 03, 2003: Message edited by: Marlene Miller ]
 
CH Lee
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,
I have done some thinking on the solution shown by Vicken. I must say this again that it's great and I am gonna use his solution, with some 'customisation' to make me understand better and hopefully the rest as well.
I find that calculating in Vicken's could lead me to more mistake (I am a very careless person ), so I 'device' this method.
Vicken, please verify this for me. Thanks.
Let's take the same example.
int i = 130;
byte b = (byte) i;
Decimal 130 = Binary 10000010
In order to get the -ve value, except the left most '1', I just 'flip' all binary value (do a XOR?), then I will get:
Binary 11111101 = Decimal 253.
Because the max of =ve value is 127, so I deduct 127 from the above value.
253 - 127 = 126
And the value is -126 (must remember to put back the - sign as we are in the -ve zone).
Does this sound good?
As for value 256, (Binary 11111111), after flipping all the value, it becomes 10000000, so it's basically 0.
Thanks Vicken for this idea. If I get all questions on this kind in SCJP 1.4, then I won't have to be ashame of posting it here.
Cheers.
 
Ray Stojonic
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Marlene previously showed, the easiest (and safest) way to do this is to learn and use 2s complement.
1000 0010 == 130 only in a buffer > 8 bits, or an 8 buffer that is unsigned
(like char, for instance)
A byte is signed, so we know that the number, as a byte, is negative because the leftmost bit is on.
2s complement shows us a value of 126, so the inverse value (the value we started with) must be -126
256 == 1 0000 0000 by my count is one more bit than 8, so it doesn't fit in an 8 bit buffer. byte value == 0
1111 1111 == -1 in an signed 8 bit buffer (or 255 unsigned)

(works for signed whole number buffers of any size)
or

you be the judge
[ December 04, 2003: Message edited by: Ray Stojonic ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two ways to find the 8-bit representation of �b, if 0 <= b <= 127
or to find b, if 128 <= 8-bit representation of -b <= 255.
256 � b
~b + 1
----
For example, to find the 8-bit representation of -126?
256 � 126 == 130 == 10000010
~(01111110) + 1 == 10000001 + 1 == 10000010 == 130
----
For example, 130 represents the negative of what number?
256 � 130 == 126
~(10000010) + 1 == 01111101 + 1 == 01111110 == 126
[ December 04, 2003: Message edited by: Marlene Miller ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic