1 Saint Louis University Arithmetic and Bitwise Operations on Binary Data CSCI 224 / ECE 317: Computer Architecture Instructor: Prof. Jason Fritts Slides.

Slides:



Advertisements
Similar presentations
When NOT to use Unsigned? Don’t Use Just Because Number Nonzero – C compilers on some machines generate less efficient code unsigned i; for (i = 1; i
Advertisements

Fabián E. Bustamante, Spring 2007 Integers Today Numeric Encodings Programming Implications Basic operations Programming Implications Next time Floats.
CSCE 212 Computer Organization Lecture 2 Data Representation.
CS213 Topics Numeric Encodings Unsigned & Two’s complement Programming Implications C promotion rules Basic operations Addition, negation, multiplication.
Bits and Bytes CS 213 Aug. 27, 1998 Topics Why bits? Representing information as bits –Binary/Hexadecimal –Byte representations »numbers »characters and.
Bits and Bytes January 17, 2002 Topics Why bits? Representing information as bits –Binary/Hexadecimal –Byte representations »numbers »characters and strings.
“The course that gives CMU its Zip!” Topics Numeric Encodings –Unsigned & Two’s complement Programming Implications –C promotion rules Basic operations.
CS213 Integers Apr 3, 2006 Topics Numeric Encodings
“The course that gives CMU its Zip!” Topics Numeric Encodings Unsigned & Two’s complement Programming Implications C promotion rules Basic operations.
ICS 2005 Instructor: Peter A. Dinda TA: Bin Lin Recitation 2.
Bits and Bytes Topics Why bits? Representing information as bits Binary/Hexadecimal Byte representations »numbers »characters and strings »Instructions.
February 4, 2003 CSCE 212 Computer Architecture Lecture 3 Representing Integers.
Fabián E. Bustamante, Spring 2007 Bits and Bytes Today Why bits? Binary/hexadecimal Byte representations Boolean algebra Expressing in C.
Carnegie Mellon 1 This Week: Integers Integers  Representation: unsigned and signed  Conversion, casting  Expanding, truncating  Addition, negation,
Topics Numeric Encodings Unsigned & Two’s complement Programming Implications C promotion rules Basic operations Addition, negation, multiplication Programming.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bits, Bytes, and Integers MCS-284 : Computer Organization.
Carnegie Mellon 1 Bits, Bytes, and Integers Lecture, Jan. 24, 2013 These slides are from website which accompanies the book “Computer.
Comp Integers Spring 2015 Topics Numeric Encodings
CS 270: Computer Organization Bits, Bytes, and Integers
Lecture 2 Representations
Bits and Bytes Topics Representing information as bits Bit-level manipulations Boolean algebra Expressing in C.
Lecture 3: Computation and Representation CS 2011 Fall 2014, Dr. Rozier.
“The course that gives CMU its Zip!” Topics Basic operations –Addition, negation, multiplication Programming Implications –Consequences of overflow.
Topics Numeric Encodings Unsigned & Two’s complement Programming Implications C promotion rules Basic operations Addition, negation, multiplication Programming.
Bits and Bytes Topics Why bits? Tell me Representing information as bits Binary/Hexadecimal Byte representations »numbers »characters and strings »Instructions.
Bits, Bytes, and Integers Topics Representing information as bits Bit-level manipulations Boolean algebra Expressing in C Representations of Integers Basic.
– 1 – Number Systems Number of symbols = base of the system Most intuitive -- base 10 (decimal system) counting on fingertips symbols
CS 105 “Tour of the Black Holes of Computing” Topics Numeric Encodings Unsigned & Two’s complement Programming Implications C promotion rules Basic operations.
Bits and Bytes Boolean Algebra ( ) Boolean algebra Expressing in C.
CS 105 “Tour of the Black Holes of Computing” Topics Numeric Encodings Unsigned & Two’s complement Programming Implications C promotion rules Basic operations.
Bits and Bytes Topics Why bits? Representing information as bits
“The course that gives CMU its Zip!” Topics Basic operations –Addition, negation, multiplication Programming Implications –Consequences of overflow.
Integers Topics Representations of Integers Basic properties and operations Implications for C.
Lecture 3: Boolean Algebra and Registers CS 2011 Spring 2016, Dr. Rozier.
Carnegie Mellon Bits, Bytes, and Integers (1-2) /18-243: Introduction to Computer Systems 2 nd Lecture, 19 May 2011 Instructors: Gregory Kesden.
Lecture 2 Two’s Complement Proofs Topics Two’s complement January 11, 2016 CSCE 212H Computer Architecture.
1 Manipulating Information (1). 2 Outline Bit-level operations Suggested reading –2.1.7~
Lecture 2 Ints revisited Topics Unsigned Integers Signed magnitude Two’s complement January 11, 2016 CSCE 212H Computer Architecture.
Lecture 4: Representing Negative Numbers CS 2011 Spring 2016, Dr. Rozier.
Topics Numeric Encodings –Unsigned & Two’s complement Programming Implications –C promotion rules Basic operations –Addition, negation, multiplication.
Today’s Instructor: Randy Bryant
This Week: Integers Integers Summary
CS 367 Integers Topics (Ch. 2.2, 2.3) Numeric Encodings
Bits, Bytes, and Integers CSE 238/2038/2138: Systems Programming
Instructor: David Ferry
Integer Representations and Arithmetic
CS213 Integers Topics Numeric Encodings Programming Implications
CS 105 “Tour of the Black Holes of Computing”
CS 105 “Tour of the Black Holes of Computing”
Representing Information
CS140 Lecture 08: Data Representation: Bits and Ints
Data III & Integers I CSE 351 Winter 2017
Comp Integers Spring 2015 Topics Numeric Encodings
Bits and Bytes Topics Representing information as bits
Bits, Bytes, and Integers January 16, 2008
Bits and Bytes Topics Representing information as bits
Bits and Bytes Boolean algebra Expressing in C
Bits and Bytes Topics Representing information as bits
Bits, Bytes, and Integers 2nd Lectures
Bits and Bytes Topics Representing information as bits
“The Class That Gives CMU Its Zip!”
Bits and Bytes Topics Representing information as bits
Arithmetic Topics Basic operations Programming Implications
Comp Org & Assembly Lang
Comp Org & Assembly Lang
Computer Organization COMP 210
Operations and Arithmetic
“The Class That Gives CMU Its Zip!”
“The Class That Gives CMU Its Zip!”
Lecture 2: Bits, Bytes, Ints
Presentation transcript:

1 Saint Louis University Arithmetic and Bitwise Operations on Binary Data CSCI 224 / ECE 317: Computer Architecture Instructor: Prof. Jason Fritts Slides adapted from Bryant & O’Hallaron’s slides

2 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 A&B = 1 when both A=1 and B=1 Or 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

3 Saint Louis University Application of Boolean Algebra Applied to Digital Systems by Claude Shannon  1937 MIT Master’s Thesis  Reason about networks of relay switches  Encode closed switch as 1, open switch as 0 A ~A ~B B Connection when A&~B | ~A&B A&~B ~A&B = A^B

4 Saint Louis University General Boolean Algebras Operate on Bit Vectors  Operations applied bitwise  Bitwise-AND operator: &  Bitwise-NOR operator: |  Bitwise-XOR operator: ^  Bitwise-NOT operator: ~ All of the Properties of Boolean Algebra Apply & | ^ ~

5 Saint Louis University Representing & Manipulating Sets Representation  Width w bit vector represents subsets of {0, …, w–1}  a j = 1 if j ∈ A  { 0, 3, 5, 6 }   { 0, 2, 4, 6 }  Operations  & Intersection { 0, 6 }  | Union { 0, 2, 3, 4, 5, 6 }  ^ Symmetric difference { 2, 3, 4, 5 }  ~ Complement { 1, 3, 5, 7 }

6 Saint Louis University Bit-Level Operations in C 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// | ➙

7 Saint Louis University Contrast: Logic Operations in C 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 Saint Louis University Shift Operations Left Shift: x << y  Shift bit-vector x left y positions –Throw away extra bits on left  Fill with 0 ’s on right Right Shift: x >> y  Shift bit-vector x right y positions  Throw away extra bits on right  Logical shift  Fill with 0 ’s on left  Arithmetic shift  Replicate most significant bit on right Undefined Behavior  Shift amount < 0 or ≥ word size Argument x << Log. >> Arith. >> Argument x << Log. >> Arith. >>

9 Saint Louis University Bitwise-NOT: One’s Complement Bitwise-NOT operation: ~  Bitwise-NOT of x is ~x  Flip all bits of x to compute ~x  flip each 1 to 0  flip each 0 to 1 Complement  Given x ==  Flip bits (one’s complement): x: ~x:

10 Saint Louis University Negation: Two’s Complement Negate a number by taking 2’s Complement  Flip bits (one’s complement) and add 1 ~x + 1 == -x Negation (Two’s Complement):  Given x ==  Flip bits (one’s complement):  Add 1: x: ~x: x:

11 Saint Louis University Complement & Increment Examples x = x = 0

12 Saint Louis University Unsigned Addition Addition Operation  Carry output dropped at end of addition  Valid ONLY if true sum is within w-bit range Example #1: u v + u + v True Sum: w+1 bits Operands: w bits Discard Carry: w bits Valid in 8-bit unsigned range

13 Saint Louis University Unsigned Addition Example #2: Not Valid in 8-bit unsigned range (312 is > 255) Example #3: Not Valid in 16-bit unsigned range (70060 is > 65535)

14 Saint Louis University Visualizing (Mathematical) Integer Addition Integer Addition  4-bit integers u, v  Compute true sum  Values increase linearly with u and v  Forms planar surface u v

15 Saint Louis University Visualizing Unsigned Addition Wraps Around  If true sum ≥ 2 w  At most once 0 2w2w 2 w+1 u v True Sum Modular Sum Overflow

16 Saint Louis University Visualizing Signed Addition Values  4-bit two’s comp.  Range from -8 to +7 Wraps Around  If sum  2 w–1  Becomes negative  At most once  If sum < –2 w–1  Becomes positive  At most once u v PosOver NegOver

17 Saint Louis University Signed Addition Example #1: Valid in 8-bit signed range (-128 < 56 < 127) 111 Example #2: Note: Same bytes as for Ex #1 and Ex #2 in unsigned integer addition, but now interpreted as 8-bit signed integers Not Valid in 8-bit signed range (172 > 127)

18 Saint Louis University Arithmetic: Basic Rules Unsigned ints, 2’s complement ints are isomorphic rings: isomorphism = casting Left shift  Unsigned/signed: multiplication by 2 k  Always logical shift Right shift  Unsigned: logical shift, div (division + round to zero) by 2 k  Signed: arithmetic shift  Positive numbers: div (division + round to zero) by 2 k  Negative numbers: div (division + round away from zero) by 2 k Use biasing to fix

19 Saint Louis University Integer C Puzzles x < 0  ((x*2) < 0) ux >= 0 x & 7 == 7  (x<<30) < 0 ux > -1 x > y  -x < -y x * x >= 0 x > 0 && y > 0  x + y > 0 x >= 0  -x <= 0 x = 0 (x|-x)>>31 == -1 ux >> 3 == ux/8 x >> 3 == x/8 x & (x-1) != 0 int x = foo(); int y = bar(); unsigned ux = x; unsigned uy = y; Initialization