Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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
  • 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Screencast - Getting to Know the Pipeline

580 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