944,138 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 2428
  • C RSS
Sep 30th, 2005
0

Help with C bitwise code

Expand Post »
Could someone help me with this. I need to know how to write this in code for C.

"if hashValue(variable) has a non-zero bit in the 4 leftmost (highest) bits"

It has to do with bitwise shifting and is basically just an if statement's condition. Thanks
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
desertstorm is offline Offline
12 posts
since Sep 2005
Sep 30th, 2005
0

Re: Help with C bitwise code

This has the pungeant stench of homework, so I won't just do it for you.

Have you googled at all for C bit manipulation, or bitwise operations?

Hint: You will need to mask away the bits that you are not concerned about (the lower bits), so what mask would you use, and what bitwise operator?
Reputation Points: 114
Solved Threads: 1
Newbie Poster
shric is offline Offline
5 posts
since Sep 2005
Sep 30th, 2005
0

Re: Help with C bitwise code

I understand how to bit shift and all that, but the thing I don't understand is how you would check just the 4 leftmost bits to see if they are a non-zero bit. I have interpreted non-zero to mean 1, as in 0 or 1 in bits. I'm not sure if this is what it means but that's they way I have interpreted it. Also one way I thought about doing this was:

Since 8 bits equals 1 byte, I bit shifted the hashNumber 4 spaces to the right and that would set the four left spots to zero, then I compared them using | to "00000000", and checked to see if the output was still "00000000" and if it was, then that means the 4 leftmost bits must have all been zero's. Does this sound correct to you? Thanks for the assistance.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
desertstorm is offline Offline
12 posts
since Sep 2005
Sep 30th, 2005
0

Re: Help with C bitwise code

Nevermind my previous post, that makes no sense. I just received help on this from another person and learned that I'm way off. Thanks for your help. Greatly appreciated.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
desertstorm is offline Offline
12 posts
since Sep 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: need help with pancake sorting
Next Thread in C Forum Timeline: how to use MFC ?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC