Edited by MrWannabe, 25 September 2009 - 08:35 PM.
Bitwise Manipulations in c
Started by MrWannabe, Sep 22 2009 08:57 PM
7 replies to this topic
#1
Posted 22 September 2009 - 08:57 PM
I resolved my problem - thank you for the help!
|
|
|
#2
Posted 22 September 2009 - 09:20 PM
1. The two's complement is just the first bit of the signed integer. If you flip the bit, you negate the number. So lets say you have a signed char with a value of 1. If you were to convert that to binary it would be 00000001. If you were to negate that number (flip the leftmost bit) and turn it into -1, the binary equivalent would be 10000001.
2. A word is just a number comprised of two bytes. To get the value of the rightmost byte, you just need to clear all the bits in the leftmost byte. So if you had a word comprised of two bytes, each equaling 1, it'd look like 00000001 00000001. To get the value of the rightmost byte all you need to do is set the first 8 bits to 0. You can do this using a mask pretty easily. If you want to get the value of the leftmost byte, you need to clear all the bits in the rightmost byte, and then shift right 8 times to bring the leftmost byte into the rightmost position. You need to shift it over because the computer will interpret 00000001 00000000 a lot differently then 00000000 00000001.
2. A word is just a number comprised of two bytes. To get the value of the rightmost byte, you just need to clear all the bits in the leftmost byte. So if you had a word comprised of two bytes, each equaling 1, it'd look like 00000001 00000001. To get the value of the rightmost byte all you need to do is set the first 8 bits to 0. You can do this using a mask pretty easily. If you want to get the value of the leftmost byte, you need to clear all the bits in the rightmost byte, and then shift right 8 times to bring the leftmost byte into the rightmost position. You need to shift it over because the computer will interpret 00000001 00000000 a lot differently then 00000000 00000001.
#3
Posted 23 September 2009 - 04:56 AM
brownhead said:
1. The two's complement is just the first bit of the signed integer. If you flip the bit, you negate the number. So lets say you have a signed char with a value of 1. If you were to convert that to binary it would be 00000001. If you were to negate that number (flip the leftmost bit) and turn it into -1, the binary equivalent would be 10000001.
#4
Posted 23 September 2009 - 07:42 AM
Do you understand what the bitwise operators do?
#5
Posted 23 September 2009 - 09:25 AM
MrWannabe: Could you post links to the other forums where you also posted this question? I don't want to be answering something that's already been answered elseweb.
#6
Posted 23 September 2009 - 12:21 PM
Quote
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 limited to binary manipulation makes it rather difficult.
It sounds simple enough, but limited to binary manipulation makes it rather difficult.
Hint: use limits.h. It's also important to know two's-complement.
Quote
4. Convert from sign-magnitude to two's complement with int sm2tc(int x), where sm2tc(0x80000005) should return -5
I do not understand this byte-related problem either.
I do not understand this byte-related problem either.
There's two ways of representing a signed integer: sign-magnitude, and two's-complement. Take the following examples:
1: 0x00000001 (unsigned, both two's-complement and sign-magnitude)
-1: 0x80000001 (sign-magnitude)
-1: 0xFFFFFFFF (two's-complement)
The trick is to strip the magnitude (i.e. save the high bit in another variable and clear it), and then depending on if the sign bit is set, invert the magnitude and add 1 (signed) or leave it alone and return the magnitude (unsigned). This is trivial in itself; the trick is doing it without an if statement. I take it you're not allowed to use the ternary operator ?: either?
Quote
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 understand 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.
This I understand 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.
XOR all the bits together. I haven't quite figured out how to do that without a loop, though.
Edited by dargueta, 23 September 2009 - 12:22 PM.
Clarified something
sudo rm -rf /
#7
Posted 23 September 2009 - 01:02 PM
Quote
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 understand 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.
This I understand 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.
Edited by fondi, 23 September 2009 - 02:05 PM.
#8
Posted 23 September 2009 - 06:11 PM


Sign In
Create Account

Back to top









