Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login

# Primitive Casting - Long to Int

Mike Vella Zarb
Greenhorn

Joined: Dec 28, 2009
Posts: 21
Hi guys,
I'm reading the KS+BB Study Guide to SCJP 6 and I am on Chapter 3 pg 195. There is a section about casting a Long to a Byte, and there is a paragraph which I do not understand...goes like this:

You don't get a runtime error, even when the value being narrowed is too large for the type. The bits to the left of the lower 8 just...go away. If the leftmost bit (the sign bit) in the byte (or any any integer primitive) now happens to be a 1, the primitive will have a negative value.

This was written for this code:

My question is: What on earth is a sign bit? And what are they talking about when they say "the bits to the left of the lower 8..."

Mike

It is a mistake to think that you can solve any major problem with just potatoes.
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
first of all you must know that byte is of 8 bts and long is of 64 bits
so whn you casting from an long to an byte ou are casting a 64 bits into 8 bits so only the the rightmost 8 bits would be left.
now a sign bit implies a sign a+ or -
if the first bit is 1 then the value is - otherwise +
10000000 is negative because first bit is 1
00000000 is positive because first bit is 0
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
look..let me give you an example using an int 8...8 is represented as 0000000000000000000000000000001000...because an int occupies 32 bits...now you know that byte occupies only 8 bits....so what happens is the lower 8 bits from the above 32 bits are removed....and what remains is 00001000 which is again 8...this is why byte a=8 works...now try it with 128....it given in the book....in that the 8th bit of the int 128 must be 1....so when you have removed the first 24 bits...you would have a number which starts with 1....since byte is signed..so the first 1 in that number means the number is negative....

so when you say byte a=(cast)128...a actually gets the value -128...not sure but something negative..same is the case with long values..which are 64 bits

scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
Mike Vella Zarb
Greenhorn

Joined: Dec 28, 2009
Posts: 21
James Carter wrote:first of all you must know that byte is of 8 bts and long is of 64 bits
so whn you casting from an long to an byte ou are casting a 64 bits into 8 bits so only the the rightmost 8 bits would be left.
now a sign bit implies a sign a+ or -
if the first bit is 1 then the value is - otherwise +
10000000 is negative because first bit is 1
00000000 is positive because first bit is 0

Thanks for your quick answer, much appreciated. But what do you mean by rightmost 8 bits?
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
suppose you have this 0000000000000000000000000000001000 then rightmost 8 bits will be 00001000
hope you get it now
Mike Vella Zarb
Greenhorn

Joined: Dec 28, 2009
Posts: 21
James Carter wrote:suppose you have this 0000000000000000000000000000001000 then rightmost 8 bits will be 00001000
hope you get it now

I see, thanks for your help!
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
hey what about my help..... didnt my post help you?
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
OOOOO little baby is crying for appreciation
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
then what....didnt you like getting appreciated.....
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
but at least i am not asking to appreciate me
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
but you were hoping to
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
hoping and asking are different as in english
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
i thought my post would have made him understood...but instead he thanked you...
Mike Vella Zarb
Greenhorn

Joined: Dec 28, 2009
Posts: 21
Hey Raju, I appreciate your answer too! It's just I didn't understand it as well...but then, why is it that when we print the value of b, it prints -126 and not -127....isn't 127 the biggest value a byte can hold?
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
hey i was just messing around..just kidding...
Mike Vella Zarb
Greenhorn

Joined: Dec 28, 2009
Posts: 21
Raju Champaklal wrote:hey i was just messing around..just kidding...

Oh OK But do you know why it prints -126 and not -127, since -127 is the most a byte can go to?
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
look....l is 130....so after 127 the answer will go 3 values on the other side....if you dont understand this way make the 32 or 64 bit value of 130
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
Ankur dont start messing again.

SCJP 1.6 96%
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
for this i would recommend kb and look for two's complement concept which may be on same page as this question.
if you want the largest and smallest value a type can hold use Byte.MAX_VALUE (for maximum value) and Byte.MIN_VALUE(for minimum value)
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
Mike Vella Zarb
Greenhorn

Joined: Dec 28, 2009
Posts: 21
Huh? Didn't quite get that last one...
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
Oh OK But do you know why it prints -126 and not -127, since -127 is the most a byte can go to?

well actually its the least value a byte can go
Mike Vella Zarb
Greenhorn

Joined: Dec 28, 2009
Posts: 21
James Carter wrote:
Oh OK But do you know why it prints -126 and not -127, since -127 is the most a byte can go to?

well actually its the least value a byte can go

Yeah ok, least, but then why does it print -126?
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
@ raju alias ankur
i was gettin bored so i just thought to have some fun with you
no hard or bad feelings bro just enjoyinn
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
byte occupies 8 bits..so its range is -128 to 127.....now 130 is 00000000000000000000000010000010.....
Ben Power
Ranch Hand

Joined: Dec 30, 2009
Posts: 31
Raju Champaklal wrote:hey i was just messing around..just kidding...

Raju, your answer worked for me, although I already understood the 8 bit vs 64 bit issue. For the way my brain works it was just right. I think it's just an issue of different ways to reason through things...

Do you feel duly appreciated?

Ben

SCJP 6
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
same here man....m thinking why -126
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
Remember, to find out the value of a negative
number using two's complement notation, you flip all of the bits and then add 1.
Flipping the 8 bits gives us 01111111, and adding 1 to that gives us 10000000, or
back to 128! And when we apply the sign bit, we end up with –128.
You must use an explicit cast to assign 128 to a byte, and the assignment leaves
you with the value –128. A cast is nothing more than your way of saying to the
compiler, "Trust me. I'm a professional. I take full responsibility for anything weird
that happens when those top bits are chopped off."

hey bro just go on page 197 and read it completely you will understand ok
there they have given reason
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
got you all
Mike Vella Zarb
Greenhorn

Joined: Dec 28, 2009
Posts: 21
Raju Champaklal wrote:byte occupies 8 bits..so its range is -128 to 127.....now 130 is 00000000000000000000000010000010.....

oooooooh I see, in binary! Then it trims to the eight bits to the right and if the first bit is a 0, it's positive, if it's 1, then negative. The rest of the 1s and 0s form a number in binary, that being -126, am I correct? (I sure do hope so! :S)
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
that is absolutely right...was jjust trying to make you udnerstand everything
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
So finally raju is happy again
appreciate him once again
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
if you are happy and you know clap your hands....if you are happy and you know tap your legs....song goes on...
Mike Vella Zarb
Greenhorn

Joined: Dec 28, 2009
Posts: 21
I bow down to Raju and appreciate him. CLAP HANDS! Stupid me, forgot that bits are binary...blonde moment much?!
Anyways, thanks to all of you guys for your help and for sticking through my intense stupidity!

Mike
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114

OK good night to all guys
n remember AAL IZZ WELL its a song from a movie 3 IDIOTS in India the language is hindi i hope i spell it correctly
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
hey how did you know? the movie is awesome
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
well i have seen it
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
dude am in india too.....and speak hindi..
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
i know you are from jaipur ankur kothari

I agree. Here's the link: http://aspose.com/file-tools

subject: Primitive Casting - Long to Int