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.
It is a mistake to think that you can solve any major problem with just potatoes.
scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
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
It is a mistake to think that you can solve any major problem with just potatoes.
James Carter wrote:suppose you have this 0000000000000000000000000000001000 then rightmost 8 bits will be 00001000
hope you get it now
It is a mistake to think that you can solve any major problem with just potatoes.
scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
It is a mistake to think that you can solve any major problem with just potatoes.
scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
Raju Champaklal wrote:hey i was just messing around..just kidding...
It is a mistake to think that you can solve any major problem with just potatoes.
scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
SCJP 1.6 96%
scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
It is a mistake to think that you can solve any major problem with just potatoes.
Oh OK But do you know why it prints -126 and not -127, since -127 is the most a byte can go to?
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
It is a mistake to think that you can solve any major problem with just potatoes.
scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
Raju Champaklal wrote:hey i was just messing around..just kidding...
SCJP 6
scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
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."
scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
Raju Champaklal wrote:byte occupies 8 bits..so its range is -128 to 127.....now 130 is 00000000000000000000000010000010.....
It is a mistake to think that you can solve any major problem with just potatoes.
scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
It is a mistake to think that you can solve any major problem with just potatoes.
scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
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
|