Arithmetic and Bitwise Operations on Binary Data

[Pages:46]Saint Louis University

Arithmetic and Bitwise Operations on Binary Data

CSCI 2400: Computer Architecture ECE 3217: Computer Architecture and Organization

Instructor: David Ferry

Slides adapted from Bryant & O'Hallaron's slides by Jason Fritts

1

Saint Louis University

Arithmetic and Bitwise Operations

Operations

Bitwise AND, OR, NOT, and XOR Logical AND, OR, NOT Shifts Complements

Arithmetic

Unsigned addition Signed addition Unsigned/signed multiplication Unsigned/signed division

2

Basic Processor Organization

Register file (active data)

We'll be a lot more specific later...

Arithmetic Logic Unit (ALU)

Performs signed and unsigned

arithmetic

Performs logic operations Performs bitwise operations

Many other structures...

CPU

Register File

Saint Louis University

ALU

Memory

3

Saint Louis University

Boolean Algebra

Developed by George Boole in 19th Century

Algebraic representation of logic

Encode "True" as 1 and "False" as 0

And

Or

A&B = 1 when both A=1 and B=1 A|B = 1 when either A=1 or B=1

Not

~A = 1 when A=0

Exclusive-Or (Xor)

A^B = 1 when either A=1 or B=1, but not both

4

General Boolean Algebras

Saint Louis University

Operate on Bit Vectors

Operations applied bitwise

Bitwise-AND operator:

&

Bitwise-NOR operator:

|

Bitwise-XOR operator:

^

Bitwise-NOT operator:

~

01101001 01101001 01101001 & 01010101 | 01010101 ^ 01010101

0011000000000011 0011111111110011 0000111111110000

~ 01010101 1100110011001100

All of the Properties of Boolean Algebra Apply

5

Quick Check

Saint Louis University

Operate on Bit Vectors

Operations applied bitwise

Bitwise-AND operator:

&

Bitwise-NOR operator:

|

Bitwise-XOR operator:

^

Bitwise-NOT operator:

~

01100110 11110000 01101001 & 00101111 | 01010101 ^ 00001111

0010010000010110 011110101 0001111010110100

~ 00101111 1101100110001000

All of the Properties of Boolean Algebra Apply

6

Bit-Level Operations in C

Saint Louis University

Operations &, |, ~, ^ Available in C

Apply to any "integral" data type

long, int, short, char, unsigned

View arguments as bit vectors Arguments applied bit-wise

Examples (char data type):

in hexadecimal

in binary

~0x41 0xBE // ~0x00 0xFF // 0x69 & 0x55 0x41 // 0x69 | 0x55 0x7D //

~010000012 ~000000002 011010012 & 010101012 011010012 | 010101012

101111102 111111112 010000012 011111012

7

Contrast: Logic Operations in C

Saint Louis University

Contrast to Logical Operators

&&, ||, !

View 0 as "False" Anything nonzero as "True" Always return 0 or 1 Early termination

Examples (char data type):

!0x41 0x00 !0x00 0x01 !!0x41 0x01

0x69 && 0x55 0x01 0x69 || 0x55 0x01 p && *p

// avoids null pointer access

8

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download