Questions about this topic? Sign up to ask in the talk tab.

Bitwise math/Operators

From NetSec
Jump to: navigation, search
back to Bitwise math


Note: all examples in this section will be using hexadecimal and binary.

NOT

NOT is a bitwise operator that takes only ONE operand. It inverts or reverses the binary value.

Example:

 A = 1010 in binary or 10 in decimal. NOT A results in the inversion of 1010 which is 0101. Therefore NOT A = 5.

AND

c3el4.png
AND returns “TRUE” per true bit in both of the required two operands. True is 1 and false is 0. It operates bit by bit, just like NOT.

AND rules

AND compares each bit and if both bits per placeholder are true, then it returns a true for that placeholder, all else gets turned into 0.
  • 1 and 1 = 1
  • 1 and 0 = 0
  • 0 and 0 = 0

AND properties

  • anything AND'd by itself results in itself
  • anything AND'd with 0xF results in itself
  • anything AND'd with 0x0 results in 0x0

AND example

Example: 0x6 AND 0x5 = 0x4
Operation Hexadecimal Binary comment


and

6

5

0110

0101

The second and third bits are true.

The second and fourth bits are true.

=
4 0100 The second bit is the only one true in both instances (5 and 6).

AND logic table

AND 0 1 2 3 4 5 6 7 8 9 A B C D E F
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
2 0 0 2 2 0 0 2 2 0 0 2 2 0 0 2 2
3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3
4 0 0 0 0 4 4 4 4 0 0 0 0 4 4 4 4
5 0 1 0 1 4 5 4 5 0 1 0 1 4 5 4 5
6 0 0 2 2 4 4 6 6 0 0 2 2 4 4 6 6
7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
8 0 0 0 0 0 0 0 0 8 8 8 8 8 8 8 8
9 0 1 0 1 0 1 0 1 8 9 8 9 8 9 8 9
A 0 0 2 2 0 0 2 2 8 8 A A 8 8 A A
B 0 1 2 3 0 1 2 3 8 9 A B 8 9 A B
C 0 0 0 0 4 4 4 4 8 8 8 8 C C C C
D 0 1 0 1 4 5 4 5 8 9 8 9 C D C D
E 0 0 2 2 4 4 6 6 8 8 A A C C E E
F 0 1 2 3 4 5 6 7 8 9 A B C D E F

OR

c3el4.png
OR will return true bits for the respective placeholder if any of the bits are true.

OR rules

OR determines if any bits are true from the given binary operands
  • 1 or 1 = 1
  • 1 or 0 = 1
  • 0 or 0 = 0

OR properties

  • Anything OR'd with itself results in itself
  • Anything OR'd with 0xF results in 0xF
  • Anything OR'd with 0x0 results in itself

OR example

Example: 5 OR C = D
Operation Hexadecimal Binary comment


or

5

C

0101

1100

The second and fourth bits are true.

The first and second bits are true.

=
D 1101 The first, second and fourth bits are true in at least one instance.

OR logic table

OR 0 1 2 3 4 5 6 7 8 9 A B C D E F
0 0 1 2 3 4 5 6 7 8 9 A B C D E F
1 1 1 3 3 5 5 7 7 9 9 B B D D F F
2 2 3 2 3 6 7 6 7 A B A B E F E F
3 3 3 3 3 7 7 7 7 B B B B F F F F
4 4 5 6 7 4 5 6 7 C D E F C D E F
5 5 5 7 7 5 5 7 7 D D F F D D F F
6 6 7 6 7 6 7 6 7 E F E F E F E F
7 7 7 7 7 7 7 7 7 F F F F F F F F
8 8 9 A B C D E F 8 9 A B C D E F
9 9 9 B B D D F F 9 9 B B D D F F
A A B A B E F E F A B A B E F E F
B B B B B F F F F B B B B F F F F
C C D E F C D E F C D E F C D E F
D D D F F D D F F D D F F D D F F
E E F E F E F E F E F E F E F E F
F F F F F F F F F F F F F F F F F

XOR

c3el4.png
XOR is a bitwise comparison operator that returns a true bit if the compared bits in question are different. If they are the same, it returns a false bit.

XOR rules

XOR determines which bits differ in the two binary numbers used as operands.
  • 1 xor 1 = 0
  • 1 xor 0 = 1
  • 0 xor 0 = 0

XOR properties

  • Anything xor'd with itself results in 0
  • Anything xor'd with 0xF is the same as a "not"
  • Anything xor'd with zero results in itself

XOR example

Example: A xor F = 5
Operation Hexadecimal Binary comment


xor

A

F

1010

1111

The first and third bits are true.

The first, second, third, and fourth bits are true.

=
5 0101 The second and fourth bits are true in ONLY one instance as opposed to two.
  • The 8’s and 2's placeholders are the same so they return 0.
  • The 4’s and 1’s placeholders are different, therefore return true.

XOR logic table

XOR 0 1 2 3 4 5 6 7 8 9 A B C D E F
0 0 1 2 3 4 5 6 7 8 9 A B C D E F
1 1 0 3 2 5 4 7 6 9 8 B A D C F E
2 2 3 0 1 6 7 4 5 A B 8 9 E F C D
3 3 2 1 0 7 6 5 4 B A 9 8 F E D C
4 4 5 6 7 0 1 2 3 C D E F 8 9 A B
5 5 4 7 6 1 0 3 2 D C F E 9 8 B A
6 6 7 4 5 2 3 0 1 E F C D A B 8 9
7 7 6 5 4 3 2 1 0 F E D C B A 9 8
8 8 9 A B C D E F 0 1 2 3 4 5 6 7
9 9 8 B A D C F E 1 0 3 2 5 4 7 6
A A B 8 9 E F C D 2 3 0 1 6 7 4 5
B B A 9 8 F E D C 3 2 1 0 7 6 5 4
C C D E F 8 9 A B 4 5 6 7 0 1 2 3
D D C F E 9 8 B A 5 4 7 6 1 0 3 2
E E F C D A B 8 9 6 7 4 5 2 3 0 1
F F E D C B A 9 8 7 6 5 4 3 2 1 0