programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# assigning short to byte

Ranch Hand
Posts: 257
Hi Friends,
(1)
public class ttest
{
public static void main(String args[])
{
final short s = 200;
byte b=(byte)s;
System.out.println("b "+b);
}
}
here b value is -56. how come this value comes like this.
I think -56 in binary is 1011 1000
binary value of 200 is 1100 1000, when it is assigned to byte it has to become -72
(2) how to assign a binary value to byte field. I tried
byte b = 1001
byte b = b1001
byte b = 1001b
I got compilation errors. for these all.
(3) in the same way when I print a byte by system.out.println then it is printing as decimal. is there any way to print as binary?
(4) Theoritiacally decimal 1 is 0000 0001 in binary. so -1 has to be
1000 0001. But it seems in practical it is 1111 1111 ???
Can some body help me.

Corey McGlone
Ranch Hand
Posts: 3271
2. From the JLS, §3.10.1 Integer Literals:

An integer literal may be expressed in decimal (base 10), hexadecimal (base 16), or octal (base 8)...

You can use Integer.parseInt(String s, int radix) to get around that.
3. Integer.toBinaryString
Corey

Ranch Hand
Posts: 257
now I understood 2's complement. But still I did not understand above issue(1).

Lionel Orellana
Ranch Hand
Posts: 87

(1)

here b value is -56. how come this value comes like this.
I think -56 in binary is 1011 1000
binary value of 200 is 1100 1000, when it is assigned to byte it has to become -72
(4) Theoritiacally decimal 1 is 0000 0001 in binary. so -1 has to be
1000 0001. But it seems in practical it is 1111 1111 ???

(4) Negative numbers in binary are not as easy as just putting a 1 in the sign bit. There's a nice little formula I find very helpful for this:
-X = ~X + 1 (sometimes shown as ~X = -X - 1 wich is useful when you want ~X)
If you want the decimal value of a negative binary like 1000 0001 you just:
• invert all the bits -> 0111 1110
• get the decimal value of 0111 1110 as normal -> 126
• add 1 and put - infront -> -127

• So what is 1111 1111?
invert -> 0000 0000
decimal -> 0
add 1 and put - -> -1!!
(1) With the formula this one's suddenly easy. 200 as a short is
0000 0000 1100 1000. When we cast it to byte we're left with 1100 1000, where the left most 1 means it's a negative number.
invert -> 0011 0111
decimal -> 55
add 1 put - infront -> -56
That's what b holds, -56.
Hope that helps
Cheers
L.
[ April 13, 2004: Message edited by: Lionel Orellana ]
[ April 13, 2004: Message edited by: Lionel Orellana ]
[ April 13, 2004: Message edited by: Lionel Orellana ]
[ April 13, 2004: Message edited by: Lionel Orellana ]
[ April 13, 2004: Message edited by: Lionel Orellana ]

Corey McGlone
Ranch Hand
Posts: 3271
now I understood 2's complement. But still I did not understand above issue(1).

Well, let's go through it, then.
200 in binary (as a 2 byte short) looks like this:
00000000 11001000
Now, if we cast that as a byte, we're going to truncate the most significant byte, which leave us with this:
11001000
the leftmost bit (the sign bit) is a 1, so we know that this is a negative number. Therefore, in order to determine what this value is, we must convert this from two's complement. To do that, we invert the bits and add 1.
00110111 - Inverted Bits