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

Recommended Answers

All 3 Replies

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?

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.