• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

Primitive Casting - Long to Int

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..."

Thanks in advance for your answers,
Mike




 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Mike Vella Zarb
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
suppose you have this 0000000000000000000000000000001000 then rightmost 8 bits will be 00001000
hope you get it now
 
Mike Vella Zarb
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey what about my help..... didnt my post help you?
 
Phungsuk Wangdu
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OOOOO little baby is crying for appreciation
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
then what....didnt you like getting appreciated.....
 
Phungsuk Wangdu
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but at least i am not asking to appreciate me
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but you were hoping to
 
Phungsuk Wangdu
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hoping and asking are different as in english
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i thought my post would have made him understood...but instead he thanked you...
 
Mike Vella Zarb
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey i was just messing around..just kidding...
 
Mike Vella Zarb
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankur dont start messing again.
 
Phungsuk Wangdu
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Mike Vella Zarb
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Huh? Didn't quite get that last one...
 
Phungsuk Wangdu
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ 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
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
byte occupies 8 bits..so its range is -128 to 127.....now 130 is 00000000000000000000000010000010.....
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
same here man....m thinking why -126
 
Phungsuk Wangdu
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
got you all
 
Mike Vella Zarb
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that is absolutely right...was jjust trying to make you udnerstand everything
 
Phungsuk Wangdu
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So finally raju is happy again
appreciate him once again
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Phungsuk Wangdu
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey how did you know? the movie is awesome
 
Phungsuk Wangdu
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well i have seen it
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dude am in india too.....and speak hindi..
 
Phungsuk Wangdu
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i know you are from jaipur ankur kothari
 
First, you drop a couch from the plane, THEN you surf it. Here, take this tiny ad with you:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic