[ACCEPTED]-Casting long to byte in Java-overrun

Accepted answer
Score: 15

A byte is a sequence of 8 bits, which makes 50 2^8 cases = 256. Half of them represent 49 negative numbers, which is -128 to -1. Then 48 there is the 0, and about the half, 1 to 47 127 represent the positive numbers.

130 as 46 Int looks like 128 + 2 which is:

0000:0000 1000:0000 (128) 
0000:0000 0000:0010 (2) 
0000:0000 1000:0010 (130) 

However, the 45 Byte has just 8 digits, and the assignment 44 takes just the bits as they are, but just 43 the last ones:

1000:0010 

The first bit indicates, it 42 is a negative number. Now how much do you 41 need to add to get to zero? Let's do it 40 stepwise:

1000:0010 x + 
0000:0001 1 = 
----------------
1000:0011 (x+1) 

1000:0011 (x+1) +
0000:0001 1 = 
----------------
1000:0100 (x+2) 

Lets do bigger steps. Just add 39 1s where we have zeros, but first we go 38 back to x:

1000:0010 x + 
0111:1101 y = 
--------------
1111:1111 

Now there is the turning point: we 37 add another 1, and get zero (plus overflow)

1111:1111 (x + y) + 
0000:0001 1
--------- 
0000:0000 0

If 36 (x+y) + 1 = 0, x+y = -1. A minus 1 is, interestingly, not 35 just the same as 1 (0000:0001) with a 'negative-flag' set 34 ('1000:0001'), but looks completely different. However, the 33 first position always tells you the sign: 1 32 always indicates negative.

But what did 31 we add before?

0111:1101 y = ?

It doesn't have a 1 at the 30 first position, so it is a positive value. We 29 know how to deconstruct that?

 ..f:8421 Position of value (1, 2, 4, 8, 16=f, 32, 64 in opposite direction)
0111:1101 y = ?
 ..f 84 1 = 1+4+8+16+32+64= 125

And now it's 28 clear: x+125 = -1 => x = -126

You may imagine 27 the values, organized in a circle, with 26 the 0 at the top (high noon) and positive 25 values arranged like on a clock from 0 to 24 5 (but to 127), and the turning point at 23 the bottom (127 + 1 => -128 [sic!].) Now 22 you can go on clockwise, adding 1 leads 21 to -127, -126, -125, ... -3, -2, -1 (at 20 11 o'clock) and finally 0 at the top again.

For 19 bigger numbers (small, int, long) take bigger 18 clocks, with the zero always on top, the 17 maximum and minimum always on bottom. But 16 even a byte is much too big, to make a picture, so 15 I made one of a nibble, a half-byte:

bitpatterns of integer, arranged in circle form

You 14 can easily fill the holes in the picture, it's 13 trivial!

Btw.: the whole thing isn't called 12 casting. Casting is only used between Objects. If 11 you have something, which is in real a subtype:

 Object o = new String ("casting or not?"); 

this 10 is just an assignment, since a String is 9 (always) an Object. No casting involved.

 String s = (String) o; 

This 8 is a casting. To the more specific type. Not 7 every object is a String. There is a small 6 relationship to integer promotion, since 5 every byte can be lossless transformed to 4 long, but not every long to byte. However, even 3 Byte and Long, the Object-types, aren't 2 inherited from each other.

You just don't 1 get a warning, for

byte s = (byte) 42;
long o = s; // no problem, no warning
byte b = (byte) o; // written like casting 
Score: 9

Bytes are signed in Java - so the range 4 of values is -128 to 127 inclusive.

The bit 3 pattern for 130 as a long, when simply truncated 2 to 8 bits, is the bit pattern for -126 as 1 a byte.

As another example:

int x = 255;
byte b = (byte) x; // b is now -1
Score: 2

You mean byte b = (byte)l?

Java's types are signed, so bytes 1 allow numbers between -128 and +127.

Score: 1

For beginners to understand: 1 byte = 8 9 bits Range (derived from 2's complement 8 no. system) = [-2^(n-1) to 2^(n-1)-1], where 7 n is no. of bits So range is -128 to 127

Whenever 6 value is incremented more than highest possible 5 +ve value, the flow goes to the lowest possible 4 -ve value.

So after value reaches 127 , flow 3 continues from -128 to -127 to -126 to cover 2 a total space of 130 and the thus o/p is 1 -126

More Related questions