I am writing a function with parameters:
int nbit2s(long int x, long int n){
}
I am looking to take in 64 bit number x, and find if a 2 bit representation of n bit length is possible. I am however restricted to using only bitwise operators and excluded from using operators such as >= <=, and conditional statements
For example, nbit2s(5,3) returns 0 because the representation is impossible.
I'm not looking for any code but just for ideas, so far my idea has been:
- Take in number n and convert it to it's binary representation.
- Left Shift the binary representation 64-n times to get MSB and store that in a variable shift 3.Shift to the right 64-n to get leading bit and store in shift
- XOR original number with W, if 1 then TRUE, 0 then FALSE.
I feel like this is along the right lines, but if someone could explain perhaps a better way to do it or any mistakes i may have made, that would be great.