# What does this mean in this variable ?

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);
###### 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.

Commented:
Hi:

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.

Glenn
0

Experts Exchange Solution brought to you by

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

Commented:
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).
Glenn
0
Commented:
dekoay:
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,
Glenn
0
Author Commented:
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.
0
Commented:
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.
Glenn
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
System Programming

From novice to tech pro — start learning today.