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

Bitwise math/Shift and rotate

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


Each of these operations can occur either to the right or to the left. Examples will use nibbles as operands (4 bits or 0.5 bytes).

Logical Shifts

For logical shifts, the bytes are assumed to be in big endian.

Using A or 1010 as an example:

Example: A shifted left by 1 = 4
Operation Hexadecimal Binary comment


Left Shift

A

1

1010

0001

shifted to the left once, the 1 at the beginning gets shifted off, thus the binary value becomes 0100, or 4 in hexadecimal.
= 4 0100 The end value 0 is always used to replace the residual numbers.


Example: A shifted left by 2 = 8
Operation Hexadecimal Binary comment


Left Shift

A

2

1010

0001

The first `10' is shifted off and the remaining two placeholders get zeroed out.
= 8 1000 The end value 0 is always used to replace the residual numbers.




Example: A shifted right by 1 = 5
Operation Hexadecimal Binary comment


Right Shift

A

1

1010

0101

a single shift to the right is division by two for even numbers.
= 5 0101 The end value 0 is always used to replace the residual numbers.


If 'A' is shifted to the right twice it goes from 1010 to 0010

Operation Hexadecimal Binary comment


Right Shift

A

2

1010

0001

shifted to the right twice, the one at the beginning gets shifted to the 2nd digit and the one in the 2nd digit place gets shifted off, thus the binary value becomes 0010, or 2 in hexadecimal.
= 2 0010 The end value 0 is always used to replace the residual numbers.

At this point, understanding should be emerging into real hacking and cryptography. If understanding is not attained regarding this knowledge, further extension topics will prove difficult. Thus, it is considered a baseline knowledge.

Exercises:

1) Solve for B left shift one


Operation Hexadecimal Binary


Left Shift One

B

1

"(B in Binary)"

0001

= "??" "???"

2) Solve for F right shift three

Operation Hexadecimal Binary


Right Shift three

F

3

"(F in Binary)"

0011

= "??" "???"

Answers:


1) 0110 or 6.

Operation Hexadecimal Binary


Left Shift One

B

1

1011

0001

= "6" "0110"


2) 0001 or 1.


Operation Hexadecimal Binary


Right Shift three

F

3

1110

0011

= 0001 1