0% found this document useful (0 votes)
37 views

Binary Operations Math/ Logical

The document discusses binary operations including addition, subtraction, logical operations, and shift/rotate operations. It provides examples of how to perform each operation in binary. Binary addition and subtraction follow similar rules to decimal with carrying or borrowing. Logical operations like AND, OR, XOR manipulate individual bits. Shift operations move bits left or right, while rotate operations move bits in a circular fashion without information loss.

Uploaded by

Marjorie Oro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Binary Operations Math/ Logical

The document discusses binary operations including addition, subtraction, logical operations, and shift/rotate operations. It provides examples of how to perform each operation in binary. Binary addition and subtraction follow similar rules to decimal with carrying or borrowing. Logical operations like AND, OR, XOR manipulate individual bits. Shift operations move bits left or right, while rotate operations move bits in a circular fashion without information loss.

Uploaded by

Marjorie Oro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Binary

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

So when the sum of a column is equal to or greater than


the base, we subtract the base from the sum, record the
difference, and carry one to the next column to the left.
Binary Addition
Rules
Rules:
 0 =0
+0 =1
 0 =1 (just like in decimal)
+1
 1 = 210
+0 = 102 = 0 with 1 to carry

 1 +11 + = 310
+
11
= 112 = 1 with 1 to carry
Binary Addition Example
1
Example 1: Add
Col 1) Add 1 + 0 = 1
Write 1
binary 110111 to 11100 Col 2) Add 1 + 0 = 1
Write 1
Col 3) Add 1 + 1 = 2 (10 in binary)
1 1 1 1 Write 0, carry 1
1 1 0 1 1 1 Col 4) Add 1+ 0 + 1 = 2
Write 0, carry 1
+ 0 1 1 1 0 0 Col 5) Add 1 + 1 + 1 = 3 (11 in
binary)
1 0 1 0 0 1 1 Write 1, carry 1
Col 6) Add 1 + 1 + 0 = 2
Write 0, carry 1
Col 7) Bring down the
Binary Addition
Verification
You can always check your
answer by converting the
figures to decimal, doing the
addition, and comparing the
answers.

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

So when you cannot subtract, you borrow from the


column to the left.
The amount borrowed is 1 base unit,
which in decimal is 10.
The 10 is added to the original column value,
so you will be able to subtract.
Binary
Explanatio
Subtraction
n
1–0=1
0 – 1 = 1 ( borrow 1 from next high
order digit)
1-1 = 0
0–0=0
Binary Subtraction
Example 1
Col 1) Subtract 1 – 0 = 1
Example 1: Subtract Col 2) Subtract 1 – 0 =
1
binary 11100 from 110011 Col 3) Try to subtract 0 – 1  can’t.
Must borrow 2 from next column.
But next column is 0, so must go to
column after next to borrow.
2 1 Add the borrowed 2 to the 0 on the right.
0 0 2 2 Now you can borrow from this
column (leaving 1 remaining).
1 1 0 0 1 1 Add the borrowed 2 to the original 0.
Then subtract 2 – 1 = 1
- 1 1 1 0 0 Col 4) Subtract 1 – 1 = 0
to subtract 0 – 1  can’t.
1 0 1 1 1 Col 5) Try Must borrow from next column.
Add the borrowed 2 to the remaining
0.
Then subtract 2 – 1 = 1
Binary Subtraction
Verification
Subtract binary
11100 from 110011:

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

 OR Results in 1 if either or both of the


operands are
1.
 OR Table
0 OR 0 = 0
0 OR 1 = 1
1 OR 0 = 1
1 OR 1 = 1
Logical OR
Operation
To perform the OR operation, take one
column at a time and perform the OR
operation using the OR table.

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

 The exclusive OR. Similar to OR except that


it now gives 0 when both its operands are 1.

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

 AND yields 1 only if both its operands are 1.

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,

SHIFT and ROTATE operations allow bits


to be moved left or right without
changing their values.
Shift Left
SHL
operation
SHL (shift left) shifts each bit one place to the left.
The original leftmost bit is lost and a 0 is shifted into
the rightmost position.

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

You might also like