Logical operations
AND OR NOT XOR
AND,OR,NOT,XOR
• Logical operations are the operations that have its result
g
p
p
as a true or false.
• The logical operations can be:
• Unary operations that has only one operand (NOT)
• Unary operations that has only one operand (NOT)
• ex. NOT operand
• Binary operations that has two operands (AND,OR,XOR)
• ex.
Logical operations
AND OR NOT XOR
AND,OR,NOT,XOR
• Operands of logical operations can be:
Operands of logical operations can be:
- operands that have values true or false
- operands that have binary digits 0 or 1 (in this
- operands that have binary digits 0 or 1. (in this
case the operations called bitwise operations).
• In computer programming a
In computer programming ,a
bitwise operation
bitwise operation
Truth tables
•
The following tables (truth tables ) that shows the result of logical
operations that operates on values true, false.
Z=x AND y y x F F F F T F
Z=x OR y y x F F F T T F F F T T T T T T F T F T T T T
Z=x XOR y y
x
F F
F x NOT X
Bitwise
Operations
• In computer programming ,a bitwise operation operates on one or two bit patterns or binary numerals at the level of their individual bits.
• Bitwise operators
• NOTNOT
• The bitwise NOT, or complement, is an unary operation that performs logical negation on each bit, forming the ones' complement of the given binary value. Digits which were 0 become 1, and vice versa. For example:
• NOT 0111 (decimal 7) = 1000 (decimal 8)NOT 0111 (decimal 7) 1000 (decimal 8)
• In many programming languages (including those in the C family), the bitwise NOT operator is "~" (tilde). This operator must not be confused with the
"logical not" operator, "!" (exclamation point), • OROR
• A bitwise OR takes two bit patterns of equal length, and produces another one of the same length by matching up corresponding bits (the first of each; the second of each; and so on) and performing the logical inclusive OR
operation on each pair of corresponding bits. In each pair, the result is 1 if the p p p g p , first bit is 1 OR the second bit is 1 OR both bits are 1, and otherwise the result is 0. For example:
• 0101 (decimal 5) OR 0011 (decimal 3) = 0111 (decimal 7)
Bitwise Operations
•
AND
• A bitwise AND takes two binary representations of equal length and
performs the logical AND operation on each pair of corresponding bits. In
p g p p p g
each pair, the result is 1 if the first bit is 1 AND the second bit is 1. Otherwise, the result is 0. For example:
• 0101AND 0011 = 0001
• In the C programming language family the bitwise AND operator is "&" • In the C programming language family, the bitwise AND operator is &
(ampersand).
•
XOR
• A bitwise exclusive or takes two bit patterns of equal length and performs • A bitwise exclusive or takes two bit patterns of equal length and performs
the logical XOR operation on each pair of corresponding bits. The result in each position is 1 if the two bits are different, and 0 if they are the same. For example:
0101 XOR 0011 0110 • 0101 XOR 0011 = 0110
Bitwise Operations
• Bit shifts
• In this operation, the digits are moved, or shifted ,to the left or right .
Registers in a computer processor have a fixed number of available bits for storing numerals, so some bits will be "shifted out" of the register at one end, while the same number of bits are "shifted in" from the other end; the differences between bit shift operators lie in how they compute the values of those shifted-in bits.
those shifted in bits. • Arithmetic shift
• In an arithmetic shift, the bits that are shifted out of either end are
discarded. In a left arithmetic shift, zeros are shifted in on the right; in a right , g ; g arithmetic shift, the sign bit is shifted in on the left, thus preserving the sign of the operand. This example uses an 8-bit register:
Bitwise Operations
Bitwise Operations
• Logical shift
Logical shift
• In a
logical shift
,the bits that are shifted out are
discarded, and zeros are shifted in (on either
,
(
end). Therefore, the logical and arithmetic
left-shifts are exactly the same operation. However,
the logical right-shift inserts bits with value 0
instead of copying in the sign bit. Hence the
logical shift is suitable for unsigned binary
logical shift is suitable for unsigned binary
numbers, while the arithmetic shift is suitable for
signed two's complement binary numbers
Bitwise Operations
Bitwise Operations
Bitwise Operations
Bitwise Operations
•
Shifts in C, C++ and Java
I C i
i d l
h l f
d i h
hif
"
"
•
In C-inspired languages, the left and right shift operators are
"
>>
"
and
,
"
<<
"
respectively. The number of places to shift is given as the
second argument to the shift operators. For example,
•
x = y << 2;
y
;
•
assigns
x
the result of shifting
y
to the left by two bits.
•
In C and C++, computations with the left operand as an unsigned
integer use logical shifts.
I C th
lt
ith th l ft
d
i
d i t
•
In C, the results with the left operand as a signed integer are:
•
for
:"
>>
"
y×2
left•
for
:"
<<
"
y/2
right.
•
In Java all integer types are signed and the
"
>>
"
and
"
<<
"
•