• No results found

Logicalandbitwiseoperations.pdf

N/A
N/A
Protected

Academic year: 2020

Share "Logicalandbitwiseoperations.pdf"

Copied!
9
0
0

Loading.... (view fulltext now)

Full text

(1)

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.

(2)

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

(3)

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

(4)

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)

(5)

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

(6)

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:

(7)

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

(8)

Bitwise Operations

Bitwise Operations

(9)

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

"

<<

"

In Java ,all integer types are signed, and the >> and <<

References

Related documents

 Computer systems store information electronically using bits (binary digits).. • Each bit can be in one of two states, which we can take to represent the binary (base-2) digits

Every finite Boolean algebra is isomorphic to a Boolean algebra with elements being bit vectors of finite length with bitwise operations OR and AND. Two Boolean algebras with

Actual electricity generation data from the installed Celentano Hall PV system

Reacting to this growth in small business email deployments, Microsoft now has specifically targeted solutions (e.g., Small Business Server, which includes the Exchange

In order to define PABP’s requirements to associate with VP- and BC200 RNA, protein/RNA interaction studies have been performed with recombinant proteins containing pairwise

&lt;&lt; &gt;&gt; Bitwise shift left, Bitwise shift right left-to-right. &lt;

Use a Mask which is a 32-bit binary number that gives the first address in the block (the network address) when bitwise ANDed with an address in the block... The network address

network and node parts of the address. The network bits are represented by the 1s in the mask, and the node bits are represented by the 0s. Performing a bitwise logical AND