Jump to content

Bitwise Manipulations in c

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
MrWannabe

MrWannabe

    Newbie

  • Members
  • Pip
  • 1 posts
I resolved my problem - thank you for the help!

Edited by MrWannabe, 25 September 2009 - 08:35 PM.


#2
brownhead

brownhead

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
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.

#3
fondi

fondi

    Newbie

  • Members
  • PipPip
  • 19 posts

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.
The two`s complement of a number is done simply by flipping all it`s bits and adding one, so that is ~n + 1.

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Do you understand what the bitwise operators do?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
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
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,716 posts

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.

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.

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.

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
fondi

fondi

    Newbie

  • Members
  • PipPip
  • 19 posts

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.
you can use !(x & (x-1)) I think. if one bit only is on that means the number is a power of two, then by ANDing x with its previous number we get zero if it is a power of two. by the way I have seen operator - is forbidden but you can use the two`s complement to get -1 and add it to x rather than the aforementioned.

Edited by fondi, 23 September 2009 - 02:05 PM.


#8
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,716 posts
I'm slightly ashamed I didn't think of that.
sudo rm -rf /