• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Casting byte to int

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I cant figure out how the output is -128 for the following code:


I know that 128 would be converted to binary which comes out to be 10000000 and then you discard the 3 most significant bits and then what ever is left, the most significant bit of that number is the sign bit. But then for this e.g it turns out to be 00000000 and not -128.
Anyone !!!
Thanks
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi faqeer,

where did you get the discard-the-trhee-most-significiant-bits-rule from? according to my knowledge, for integer data types except char, all least significiant bits that can be held by the new narrower data type are simply taken over.
in this case, b holds the same bit pattern 10000000, which is -128.

hope it helps
greetings
[ January 27, 2006: Message edited by: Tilo Hemp ]
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
byte value ranges from -128 to 127, ie -2^n to 2^(n-1)

here ^ -->> power of
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Faqeer,

128 is int - 4 bytes number. If you cast it to byte the 3 most significant bytes not bites are discarded.
 
Faqeer Khudaka
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually I missed out the fact that 128 is an int and that means 32 bits.

Anyway, thanks for the help.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can any one provide complete information regarding this topic??(I mean links to this topic)
 
ven kaar
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
probably this helps

http://www.garba.org/documents/java2/casting.html
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for doc. this is really good.

but in the following

int a = 129;
byte x = (byte)a;

how is Result: -127 ?

129 binary representation is 10000001. how is this -127. Please explain
 
Anju sethi
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for doc. this is really good.

but in the following

int a = 129;
byte x = (byte)a;

how is Result: -127 ?

129 binary representation is 10000001. how is this -127. Please explain
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, before byte is casted to 128 its binary representation is
00000000 00000000 00000000 10000000.

Now, when you cast it to byte only 8 bits are left out of 32. So the binary representation is

10000000
- 1 on the right most side means its a negative number.

To get the value of the negative binary numbers, we flip the 0s to 1s and vice versa
which leaves to
01111111 = 127
and finally add 1 to it for negation
so its equal to -128
 
Abhinav Gogna
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for 129, its the same logic as posted before

we have
00000000 00000000 00000000 10000001

after byte cast
10000001

negating since it has 1 as the right most bit leads to
011111110

which gives 126 then all you have to do is add 1 and put a negative sign in front of it
hence -127.

Hope this helps.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A simple way to cast byte for numbers greater than 127 :

Divide 256 by that no.

ex: byte b = (byte)128;
Divide 256 by 128;
Result is -128.
For 129 result is -127.
 
Anju sethi
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the great explanation
dat helped in understanding
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by amrita sankar:
A simple way to cast byte for numbers greater than 127 :

Divide 256 by that no.

ex: byte b = (byte)128;
Divide 256 by 128;
Result is -128.
For 129 result is -127.



Er... Excuse me!! did you say Divide?? or is it Substract??? Be ware of this kinda terrible Typo's. This should be Substract..

And yes, Abhinav Gogna... Hats off for you!
ven kaar, Hats off for you too!!!
[ January 29, 2006: Message edited by: Roshan Amadoru ]
 
Anju sethi
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having another doubt.

for
int a = -129;
byte b = (byte) a;

Result : 127.

How is this done.

-129 can be represented as
11111111 11111111 11111111 10000001

The doc says: if the value is greater than 255 or lower than -127, the lower byte is kept and rest is thrown away.

As per the above statement: 10000001 is kept. which is a signed byte
On taking 2's complement of this . We get -127.

Please explain how r we getting +127

thanks
Anju
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
-129 in hexadecimal is ffffff7f, not ffffff81 as you wrote.
Casting to a byte gives you 7f, which is 127.
 
Abhinav Gogna
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Binary representation of -129 is
11111111 1111111 11111111 01111111 not

11111111 11111111 11111111 10000001.

Try the 2's complement now.

Whenever in doubt, you can use Integer.toBinaryString(int) methood to convert decimal numbers to binary.
 
Faqeer Khudaka
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have got a bit confused here. Need some help.

For this code


Howz the answer coming out to 127. Can somebody explain in detail showing the full calculation, please.

Thanks
 
There's a way to do it better - find it. -Edison. A better tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic