What does this mean in this variable ?

Posted on 1999-12-20
Medium Priority
Last Modified: 2013-11-20
I saw in a code written by a programmer... but I don't know what it means. Hopefully there are some experts could tell me...

The source is as belows...

int bits = (c &0xf0)>>4;

the other one is as below...

c = (char)(~c&0xff);
Question by:dekoay
  • 4

Accepted Solution

GlennDean earned 200 total points
ID: 2296640
   c&0xf0 will take c and clear out bits 3 thru 0 in c (I'm assuming the least significant bit is bit 0).  For example:
  c=0xD7   c&0xf0 = 0xD0
  c=0x03   c&0xf0 = 0x00
The >>4 simply shifts all the bits 4 "spots" to the right.  In the above example, you would get 0x0D for the 1st example and 0x00 for the second.
   One has to be extremely careful when doing the above because behind the scenes char c is converted to a 4-byte value (sign extended) and then ANDed with 0x000000f0.  That 4-byte value is then shifted 4 bits to the right and then stored in integer bits.

   On ~c&0xff, negation (~) has precedence over & go you flip all the bits in c (for example b'10110001' negated becomes b'01001110').  Again, be careful, char c is first converted (sign extended) to a 4 byte value, then negated.  ANDing with 0xff clears all bits above 7 (i.e. bits 8 thru 31 become 0) and then the assignment to c simply moves the low 8-bits over to c.

   Obviously I cann't explain why the programmer is doing this since that is context specific.


Expert Comment

ID: 2296646
  Forget to mention that when I say "behind the scenes" I mean when you look at a disassembly of your code you'll see the assembly instructions doing the above (INTEL).

Expert Comment

ID: 2296909
   I may have gone in too much detail in my answer, but I speak from personal experience in that my misunderstanding in how the compiler handled your exact question bit me hard.  I spent over 2 days trying to figure out a problem on how come a character ANDed with a number wasn't getting the results I expected.  ONLY until I analyzed the assembly code did I realize that ANDing a character with a hex value meant the character was converted to a 4-byte value.  
   Merry X'Mas,

Author Comment

ID: 2299569
Hi Glen

Thanks for replying... however I still trying hard to figure out why the programmer put in such a way. May be it is due to the messaages send by the host via winsock is in compressed form.. may be that part is doing the decompressed mode.. I guess...

Merry Xmas to you too.

Expert Comment

ID: 2299604
Hi dekoay:
   I can't even guess about what the programmer was really doing.  
   I will say this, there does seem to be a prevalent technique of "flipping" the bits (as the programmer has done with ~c).  Somehow the flipping is related to security and just plain 'ol double checking the data received.
   I say security in the naive view that if I have a character 'a', for example, if I flip the bits in 'a' then I get something bogus.  As long as the sender and receiver know each is to suppose to flip the bits then they can communicate.  The person who doesn't know the protocol, gets garbage.

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…
Suggested Courses

592 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