Bitwise operators

Posts 110 of 10 · Page 1 of 1
Bitwise operators
I understand &, |, ^, <<, >>, ~.

Code:
(3 = 0011) & (5 = 0101) = (1 = 0001)
(3 = 0011) | (5 = 0101) = (7 = 0111)
(3 = 0011) ^ (5 = 0101) = (6 = 0110)
(3 = 0011) << 5 = (48 = 00110000)
(5 = 0101) >> 3 = (0 = 0000)
~(3 = 0011) = (12 = 1100)
So the last one is >>>. Right?
Or is there also a <<<?

Edit: cleaned up the post, + removed what I understand.
Edit 2: I just realized <<< doesn't exist and >>> only exists in Java.
<<< does not represent any bitwise operations.
_______________________________
You've given me a bunch of numbers and their binary representations. Ignoring the actual bitwise operation, let's disect the format of each line.
Code:
(3 = 0011) & (5 = 0101) = (1 = 0001)
Here, the number 3 is represented at 0011 in binary, that is, 2^0 + 2^1. Same for number 5, 2^0 + 2^2 = 5. Same for number one. Note that each number represents a bit, and 8 of these bits make up a byte. Hence, we call these bitwise operations, because they work on bits. With that done and out of the way, we'll now use the binary representations *only*, since we're talking about *bitwise* operations that work on the binary value of things here.

Code:
0011 & 0101 = 0001
0011 | 0101 = 0111
0011 ^ 0101 = 0110
0011 << 5 = 00110000
0101 >> 3 = 0000
~0011 = 1100
We'll take it line-by-line.


0011 & 0101 = 0001
This is the logical AND operation. It compares two values, and returns a 1 if both values are 1, and 0 otherwise. So,
  • 0 AND 0 = 0
  • 1 AND 0 = 0
  • 0 AND 1 = 0
  • 1 AND 1 = 1


0011
0101
_______
0001

0011 | 0101 = 0111
This is the logical OR operation. It compares two values, and returns a 1 if either of the values is 1.
  • 0 OR 0 = 0
  • 1 OR 0 = 1
  • 0 OR 1 = 1
  • 1 OR 1 = 1


0011
0101
_______
0111

0011 ^ 0101 = 0110
This is the EXCLUSIVE OR operation. Same as the logical OR operation, except the two values being compared must be mutually exclusive.

  • 0 XOR 0 = 0
  • 1 XOR 0 = 1
  • 0 XOR 1 = 1
  • 1 XOR 1 = 0 < Notice, this would return 1 in OR. However, 1 and 1 are not mutually exclusive (they are not the different aka. they are the same)


0011
0101
_______
0110

0011 << 5 = 00110000
This is a bitshift. You shift (move) the bits around. This one moves bits to the *right*. It's important to note that in most modern x86 processors you find, bitshifting will fill new bits with zero, though this isn't strictly always the case (if i remember correctly).
"0011 << 5" simply means move everything to the right by 5 bits. You will now have:
"0011 _ _ _ _ _". Underscores represent spaces that now need to be filled. Fill these with zero and you get
"001100000"

0101 >> 3 = 0000
Same goes here, except this is to the left.
"0101 >> 3" means move everything to the left by 5 bits. Things that move off the right-most boundary just "fall off". You will now have:
" 0" (the last three bits have "fallen off". Note that "0" is the same as "0000", which is the same as "0000000000000000000000" or any amount of zeros - zero is zero.

~0011 = 1100
This is the complement operator. In simple words, the opposite. A 0 turns into a 1, and a 1 turns into a 0.

0011
_______
1100
So I was mostly right?
I only got ~ wrong?

I later figured out that ~3 = -4
Because 3 is actually 0000 0011 (8-bit), 0000 0000 0000 0011 (16-bit) etc. So ~3 = 1111 1100 which is -4.
I love bit op.

& = Bitmask ex. 0xFFEF5693 and you want only the 93 at the end so you'll bitmask it by doing a & 0xFFFFFF00 with 0xFFEF5693
| = Adding ...if you have 0x00000000 and you want to add 0x90 you would | it

that's how I look at those two

and ~ is just a NOT so it will change all the bits to the opposite like he said.
Quote Originally Posted by 0xC0D3 View Post
I love bit op.

& = Bitmask ex. 0xFFEF5693 and you want only the 93 at the end so you'll bitmask it by doing a & 0xFFFFFF00 with 0xFFEF5693
| = Adding ...if you have 0x00000000 and you want to add 0x90 you would | it

that's how I look at those two

and ~ is just a NOT so it will change all the bits to the opposite like he said.
0x7FFFFFFF | 0x90 != adding.
Quote Originally Posted by freedompeace View Post


0x7FFFFFFF | 0x90 != adding.
oops I used the wrong term..more like mixing..putting into...like

0x00000000 | 0x90 = 0x00000090

and if you 0x00000090 | A0 = 0x000000B0

I use for only the first example
Sign extension of bit shifts depends on if you are shifting a signed or unsigned variable. Which is why on x86 you have shl/sal and shr/sar.

Also, why mask when you can create readable code?

Code:
union readable_flags {
  struct {
    unsigned flag_0 : 1;
    unsigned flag_1 : 1;
    unsigned flag_2 : 1;
    unsigned flag_3 : 1;
    unsigned flag_4 : 1;
    unsigned flag_5 : 1;
    unsigned flag_6 : 1;
    unsigned flag_7 : 1;
  } flags;

  unsigned long raw;
}
MSVS supports anonymous structures. This will save you a level of indirection.

Code:
readable_flags rf;

// Standard C++
rf.flags.flag_0 = 1;

// MSVS with anonymous structure
rf.flag_0 = 1;
The compiler will generate the masks for you. GCC also supports this feature as an extension.
whats the : 1 after that? I never seen that before.

hmm, masking can be use if you want to get a byte out of a dword or like if you want to convert from big Indian to little Indian. It's all Assembly, but we have C++, to make it easier. But it nice to know how it works in assembly and what it's used for, esp. when you are hacking a game.
Quote Originally Posted by 0xC0D3 View Post
whats the : 1 after that? I never seen that before.

hmm, masking can be use if you want to get a byte out of a dword or like if you want to convert from big Indian to little Indian. It's all Assembly, but we have C++, to make it easier. But it nice to know how it works in assembly and what it's used for, esp. when you are hacking a game.
The field takes up one bit.

<identifier> : 2 will use 2 bits,
<identifier> : 3 will use 3 bits, and so on.


Quote Originally Posted by 0xC0D3 View Post
whats the : 1 after that? I never seen that before.

hmm, masking can be use if you want to get a byte out of a dword or like if you want to convert from big Indian to little Indian. It's all Assembly, but we have C++, to make it easier. But it nice to know how it works in assembly and what it's used for, esp. when you are hacking a game.
OR you could do the same in C++ in a more consistent manner, just as Fovea has said.
yeah they made it easier for us!!

I been programming for a long time and I never seen anything like that before!! I seen unsigned but never : after it. I just though of something in one of my project that will make it more efficient using that, thanks!
Posts 110 of 10 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?