# bytes in Java

Sarojini Venky

Greenhorn

Posts: 3

posted 12 years ago

Hi guys,

This must seem extremely silly and less than basic!!! But seriously how does an int get stored in a byte data type. How does the truncation happen?

What info I have is that it divides the int(if > than 256) by 256 and stores the remainder in the byte. But what happens if I want to store a value 250?

Would be very much obliged if anyone could help me.

Thanks

This must seem extremely silly and less than basic!!! But seriously how does an int get stored in a byte data type. How does the truncation happen?

What info I have is that it divides the int(if > than 256) by 256 and stores the remainder in the byte. But what happens if I want to store a value 250?

Would be very much obliged if anyone could help me.

Thanks

Michael Morris

Ranch Hand

Posts: 3451

posted 12 years ago

Moving this to Java in General Begginners.

*Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction.* - Ernst F. Schumacher

Eddie Vanda

Ranch Hand

Posts: 283

posted 12 years ago

Output is: i=250 b=-6

In two's complement notation used for an int, each column has a weighting of an increasing power of two, starting from the least significant bit. However, the most significant bit has a minus sign in front of its weighting.

So for the least significant 8 bits the weightings are

128, 64, 32, 16, 8, 4, 2 and 1 (8 bits)

And the representation of 250 puts 1's into these columns:

250 = 128 + 64 + 32 + 16 + 8 + 2

Now when you transfer these 8 bits into a 2's complement byte, the weightings are:

-128, 64, 32, 16, 8, 4, 2 and 1 (8 bits)

so the bit pattern for 250 in an int becomes -6

-6 = -128 + 64 + 32 + 16 + 8 + 2

Ed

Hope that is not too complicated!

Output is: i=250 b=-6

In two's complement notation used for an int, each column has a weighting of an increasing power of two, starting from the least significant bit. However, the most significant bit has a minus sign in front of its weighting.

So for the least significant 8 bits the weightings are

128, 64, 32, 16, 8, 4, 2 and 1 (8 bits)

And the representation of 250 puts 1's into these columns:

250 = 128 + 64 + 32 + 16 + 8 + 2

Now when you transfer these 8 bits into a 2's complement byte, the weightings are:

-128, 64, 32, 16, 8, 4, 2 and 1 (8 bits)

so the bit pattern for 250 in an int becomes -6

-6 = -128 + 64 + 32 + 16 + 8 + 2

Ed

Hope that is not too complicated!

The nice thing about Standards is that there are so many to choose from!