About bit masking/shifting

Posted on 2003-02-21
Medium Priority
Last Modified: 2008-03-03
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?
Question by:piou
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Accepted Solution

Leo71 earned 200 total points
ID: 7995124
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!

Expert Comment

ID: 8131422
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.  

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.

Community Support Moderator @Experts Exchange

Expert Comment

ID: 8153743
I will not be addressing this request until you provide a response to the site administrator in:

Community Support Moderator @Experts Exchange

Expert Comment

ID: 9447587
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 
Post your closing recommendations!  No comment means you don't care.

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Six Sigma Control Plans

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question