I'm having some problems with some concepts.
Using no loops nor conditionals, I want to understand how to work with bitwise manipulation using only these operations: <<, >>, +, |, ^, &, ~, !, =
Some things I wanted to do are:
1. Negate a number with int negate(int x), such as negate(4) returns -4
I'm not quite sure how to do the necessary two's complement in order to turn the number into a negative.
2. Extract byte n from word x with int getbyte(int x, int n), such as getbyte(0x12345678,1) will return 0x56
I'm not sure how to approach this.
3. Find if x <= y with int islessorequal(int x, int y), return 1 if true and 0 if false, such as islessorequal(3,5) should return 1
It sounds simple enough, but without "<=", it makes it a lot harder.
4. Convert from sign-magnitude to two's complement with int sm2tc(int x), where sm2tc(0x80000005) should return -5
I'm not sure how to approach this either.
5. Find if x is a power of 2 in ispower2(int x) and return 1 if true and 0 if false - as such where ispower2(16) returns 1 and ispower2(15) returns false
This I know would involve making sure the number contains only one bit with a 1, but I am not sure how to manipulate it without a conditional to discover that sole bit.
Most of these are more binary math-related problems rather than c problems, so it seems to make most sense to find the way to convert it with those limited 9 operations first before putting it in c.
Any help would be appreciated - thank you!