Binary Operations Math/ Logical
Binary Operations Math/ Logical
Operations
Math/
Logical
Binary
Math
Decimal Addition
Example 1) Add 8 + 7 = 15
Add 3758 to 4657: Write down 5, carry 1
2) Add 5 + 5 + 1 = 11
111 Write down 1, carry 1
3758
3) Add 7 + 6 + 1 = 14
+ 4657 Write down 4, carry 1
8415 4) Add 3 + 4 + 1 = 8
Write down 8
Decimal Addition
Explanation
111
3758
+ 4657
8415
1 1 0 1 1 1
+ 0 1 1 1 0 0
1 0 1 0 0 1 1
Binary Addition Example
2
Example 2:
Add 1111 to 111010.
1
1 1 1
1 1 1 0 1 0
+ 0 0 1 1 1 1
1 0 0 1 0 0 1
Decimal
Exampl
Subtraction
e
Subtract 1) Try to subtract 5 – 7 can’t.
Must borrow 10 from next column.
4657 from 8025: Add the borrowed 10 to the original 5.
Then subtract 15 – 7 = 8.
2) Try to subtract 1 – 5 can’t.
Must borrow 10 from next
7 9 11 column.
But next column is 0, so must go to
- 8 0 52 7
4 16 column after next to borrow.
Add the borrowed 10 to the original 0.
315 3 6 Now you can borrow 10 from this column.
8 Add the borrowed 10 to the original 1..
Then subract 11 – 5 = 6
3) Subtract 9 – 6 = 3
4) Subtract 7 – 4 = 3
Decimal
Explanation
Subtraction
8 0 2 5
- 4 6 5 7
3 3 6 8
2 1
0 0 2 2
1 1 0 0 1 1
- 1 1 1 0 0
1 0 1 1 1
Binary Subtraction
Example 2
Example 2: Subtract
binary 10100 from 101001
0 2 0 2
1 0 1 0 0 1
- 1 0 1 0 0
1 0 1 0 1
Logical, Shift, and
Rotate Operations
Logical, Shift and
Operation
Rotate
s
A particular bit, or set of bits, within the byte can be set to
1 or 0, depending on conditions encountered during the
execution of a program.
When so used, these bits are often called "flags".
Frequently, the programmer must manipulate these
individual bits - an activity sometimes known as "bit
twiddling".
The logical, shift, and rotate operations provide the
means for manipulating the bits.
Logical OR
OR Rules
Operations
Ex 1: 10010011
OR 0 0 0 0 1 1 1 1
10011111
Logical OR
Examples
Ex 2: 11001001
OR 0 0 0 0 1 0 1 0
11001011
Ex 3: 0111
OR 0 0 1 0
0111
Logical XOR
Rules
XOR Operations
Rules.
0 XOR 0=0
0 XOR 1=1
1 XOR 0=1
1 XOR 1=0
Logical XOR
Examples
Ex 1: 10011001
XOR 0 0 0 0 1 1 1
1
10010110
Ex 2: 0111
XOR 0 0 1
0
0101
Logical AND
Rules
AND Operations
Rules.
0 AND 0 = 0
1 AND 1 = 0
2 AND 0 = 0
1 AND 1 = 1
Logical AND
Examples
Ex 1: 11010011
AND 0 0 0 0 1 1 1 1
00000011
Ex 2: 0111
AND 1 0 0 1
0001
Logical
NOT
NOT Operations
NOT is a separate operator for flipping the bits.
Rules.
NOT 0 = 1
NOT 1 = 0
Example. NOT 1 0 1 0 = 0 1 0 1
Shift and Rotate
operations
Whereas logical operations allow the
changing of bit values in place,
Ex 1. SHL 1101
1 1 1001 =1010
10
Ex 2. SHL 1100
=1000
Shift Left - Multiple
Bits
SHL # bits means to shift left # times
Ex 1: SHL 3 10011100
1 0 0 1 01 01 1010 10 0 0 =1
1100000
Ex 2: SHL 2 11100110
=10011000
Shift Right
SHRoperation
SHR (shift right) shifts each bit one place to
the right. The original rightmost bit is lost and
a 0 is shifted into the leftmost position.
Ex 1. SHR 1 0 1 1
0101 =0101
Ex 2. SHR 1
001
1
Shift Right – Multiple
Bits
SHR # bits means to shift right # times
Ex 1: SHR 3 10011100
00010011 100 =0001001
1
Ex 2: SHR 2 11100110
=00111001
Arithmetic Shift
operatio
Right
n
ASR (retains rightmost sign bit)
Shifts each bit one place to the right. The original
rightmost bit is lost and a the value of the most significant
bit (leftmost bit) is shifted into the new leftmost position.
Ex 1. ASR 1 0 1 1
1101 1 =1101
Ex 2. ASR 0 0 1 1
=0001
Arithmetic Shift Right
–Multiple
Bits
ASR # bits means to arithmetic shift right # times
Ex 1: ASR 3 10011100
11110011 100 =1111001
1
Ex 2: ASR 2 01100110
=00011001
Rotate Left
ROL operation
ROL (rotate left) shifts each bit one place to the left.
The original leftmost bit is shifted into the
rightmost position. No bits are lost.
Ex 1. ROL 1 1 0 1
1011
Ex 2. ROL 1100
=1001
Rotate Left – Multiple
Bits
ROL # bits means to rotate left # times
Ex 1: ROL 3 10011100
=11100100
Ex 2: ROL 2 11100110
=10011011
Rotate Right
ROR
operation
ROR (rotate right) shifts each bit one place to the right.
The original rightmost bit is shifted into the leftmost
position. No bits are lost.
Ex 1. ROR 1 0 1 1
1101
Ex 2. ROR 0 0 1 1
=1001
Rotate Right – Multiple
Bits
ROR # bits means to rotate right # times
Ex 1: ROR 3 10011100
=10010011
Ex 2: ROR 2 11100110
=10111001