About bit masking/shifting

This is a general question, but let's talk Java for the example.

Primitive types byte and short are signed. If I'm not too wrong, the sign bit is the leftmost.

Here's some code (in Java) with simple masking:

1  byte b = -100;              
2  short s = -100;              
3  System.out.println(b);      
4  System.out.println(s);      
5  System.out.println(b & 0xFF);
6  System.out.println(s & 0xFF);
7  System.out.println((byte)(b & 0xFF));
8  System.out.println((byte)(s & 0xFF));

Masking with all 8 bits 'on' for the byte value, means (I think) that line 5 should print -100. But it prints 156.

Line 6 prints 156, which is more or less expected since the sign bit is lost(?)

Lines 7 and 8 print -100, which means that somehow the short's sign bit was shifted to the right by 8 positions.

So, what exactly is going on with masking and casting? Why I don't get -100 from line 5 and why I get -100 from line 8?
piouAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Leo71Commented:
For me a byte is an unsigned value. That means if the compiler allows to assign a value of -100, the byte should be 156 afterwards.
But the compiler should complain about your try assigning an signed value to an unsigne variable.
So I'm not surprised about line 5.
Line 6: The signed bit gets lost since it's the MSB of the higher byte (all bits of the higher byte are 1 before masking). You mask s with 0x00FF means the higher byte is lost.
Line 8 and 9 I don't understand.
If the result is casted to byte it must not have a sign in my opinion!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SpideyModCommented:
A request for deletion has been made.  I will not entertain the request for deletion until I understand why the request was made. All I know is "no solutions have been provided", yet I see an answer provided.  

piou,
The expert here has donated their time to try to answer your question.  A little feedback would have been nice prior to requesting a deletion.  Remember to always be the last person to respond to your own questions.

If you provide feedback of why you are requeting the deletion and post the solution if you found one, then the 72 hour clock will start ticking for the expert to object to the deletion.

SpideyMod
Community Support Moderator @Experts Exchange
0
SpideyModCommented:
piou,
I will not be addressing this request until you provide a response to the site administrator in:
http://www.experts-exchange.com/Web/Graphics/Photoshop/Q_20458899.html

SpideyMod
Community Support Moderator @Experts Exchange
0
CleanupPingCommented:
piou:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.