narrowing a primitive

I don't quite understand what happens when one is assigning a value too big for the primitive (same behaviour can occur when using the shift operators too). For example

byte b = (byte) 128;

What I get (I think) is this: the bitpattern is 10000000, so it takes a full 8 bits to represent. The literal 128 is an int and Java truncates the higher order bits, so from the 32bit int bitpattern what is left is the bitpattern above. Byte is signed which means that the number is negative, and to get the value of such a number one uses two's complement notation (flip the bits and add one). Flipping gives 01111111, adding 1 gives 10000000 back and here is where I get lost...to me it says -0 (the 1 gives a negative number and all that's left is seven zeros) and running the code gives -128...

That gives me another problem; how to represent the literal -128 with seven bits (since the 8:th bit isn't a value bit)

I'd really appreciate if someone could take the time to explain the above to me as if I was a three-year-old, or direct me to a website where it all is explained.

Regards

/Jesse

[1135 byte] By [jesse-swea] at [2007-10-2 13:24:08]
# 1

> left is the bitpattern above. Byte is signed which

> means that the number is negative, and to get the

> value of such a number one uses two's complement

> notation (flip the bits and add one).

To get the negative of a 2's-C number, yes.

Since this is -128, to get the negative (+128) you do that.

> Flipping gives

> 01111111, adding 1 gives 10000000 back

Because you can't represent +128 in 8 bits 2's-C

> and here is

> where I get lost...to me it says -0 (the 1 gives a

> negative number and all that's left is seven zeros)

> and running the code gives -128...

It doesn't quite work that way.

> That gives me another problem; how to represent the

> literal -128 with seven bits

You can't. Why do you think you need to?

> (since the 8:th bit

> isn't a value bit)

Yes, it is. That is, it's not JUST a sign bit. That's why 10000000 is -128 and not -0.

> I'd really appreciate if someone could take the time

> to explain the above to me as if I was a

> three-year-old,

GIMME MY TOY!

MOMMY! I'M HUNGRY!

STOP TOUCHING ME!

> or direct me to a website where it

> all is explained.

http://en.wikipedia.org/wiki/Twos_complement

jverda at 2007-7-13 11:02:35 > top of Java-index,Java Essentials,New To Java...
# 2

> > left is the bitpattern above. Byte is signed which

> > means that the number is negative, and to get the

> > value of such a number one uses two's complement

> > notation (flip the bits and add one).

>

> To get the negative of a 2's-C number, yes.

>

> Since this is -128, to get the negative (+128) you do

> that.

>

> > Flipping gives

> > 01111111, adding 1 gives 10000000 back

>

> Because you can't represent +128 in 8 bits 2's-C

In 9 bits, -128 would be 110000000, following the steps would give 010000000 for +128.

jverda at 2007-7-13 11:02:35 > top of Java-index,Java Essentials,New To Java...
# 3
> I'd really appreciate if someone could take the time> to explain the above to me as if I was a> three-year-old, or direct me to a website where it> all is explained.Grow up and start reading The Java Programming Language by Gosling and others.
jverda at 2007-7-13 11:02:35 > top of Java-index,Java Essentials,New To Java...