Logical operations ANDORNOTXORAND,OR,NOT,XOR •Loggpical operations are the o perations that have its result as a true or false. • The logical operations can be: • Unary operations that has only one (NOT) • ex. NOT operand • Binary operations that has two (AND,OR,XOR) • ex. operand 1 AND operand 2 operand 1 OR operand 2 operand 1 XOR operand 2

1 Dr.AbuArqoub Logical operations ANDORNOTXORAND,OR,NOT,XOR • Operands of logical operations can be: - operands that have values true or false - operands that have binary digits 0 or 1. (in this case the operations called bitwise operations). • In ,a bitwise operates on one or two patterns or binary numerals at the level of their individual .

2 Dr.AbuArqoub Truth tables

• The following tables (truth tables ) that shows the result of logical operations that operates on values true, false.

x y Z=x AND y x y Z=x OR y F F F F F F

F T F F T T

T F F T F T

T T T T T T

x y Z=x XOR y x NOT X F F F F T F T T T F T F T

T T F

3 Dr.AbuArqoub Bitwise Operations • In computer programming ,a operates on one or two bit patterns or binary numerals at the level of their individual bits. • Bitwise operators • NOT • The bitwise NOT, or complement, is an unary operation that performs logical 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) • In many programming languages (including those in the family), the bitwise NOT operator is "~" (). This operator must not be confused with the "logical not" operator, "!" (exclamation point), • OR •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 oppppgp,eration on each pair of corresponding bits. In each pair, the result is 1 if the 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) • In the C & Java ppgrogrammin g lan gua ge famil y, the bitwise OR o perator is " |" (pipe).

4 Dr.AbuArqoub Bitwise Operations • AND •A bitwise AND takes two binary representations of equal length and ppgerforms the logical AND oppppgeration on each pair of corresponding bits. In 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: • 0101 AND 0011 = 0001 • In the C family, the bitwise AND operator is "&" (). • XOR • A bitwise 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 • In the C programming language family, the bitwise XOR operator is "^" (caret).

5 Dr.AbuArqoub 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 . • shift • In an , the bits that are shifted out of either end are discarded. In a left arithmetic shift,,g;g zeros are shifted in on the right; in a right 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: • 00010111 LEFT-SHIFT = 00101110 • 00010111 RIGHT-SHIFT = 00001011

6 Dr.AbuArqoub Bitwise Operations

•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 numbers, while the arithmetic shift is suitable for signed two' s complement binary numbers.

7 Dr.AbuArqoub Bitwise Operations

8 Dr.AbuArqoub Bitwise Operations

• Shifts in C, C++ and Java • ICIn C-iidlinspired languages, t hlfdihhifhe 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; • 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. • In C , th e result s with the le ft operan as a s igne d in teger are: •for :">>" y×2left •for :"<<" y/2right . • In Java ,all integer types are signed, and the ">>" and "<<" operators perform arithmetic shifts. Java adds the operator "to<<<" perform logical right shifts

9 Dr.AbuArqoub