# Arithmetic and Bitwise Operations on Binary Data

Size: px
Start display at page:

Transcription

1 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

2 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

3 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 ALU Memory 3

4 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

5 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

6 Quick Check 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 6

7 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 // ~ x69 & 0x55 0x41 // & x69 0x55 0x7D //

8 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

9 Bitwise Operations: Applications Bit fields One byte can fit up to eight options in a single field Example: char flags = 0x1 0x4 0x8 = Test for a flag: if ( flags & 0x4 ){ //bit 3 is set } else { //bit 3 was not set } 9

10 Shift Operations Left Shift: x << y Shift bit-vector x left y places 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. >>

11 Quick Check Left Shift: x << y Shift bit-vector x left y places 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. >>

12 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 == x: Flip bits (one s complement): ~x:

13 Signed Integer 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 == x: Flip bits (one s complement): Add 1: ~x: x:

14 Complement & Increment Examples x = Decimal Hex Binary x B 6D ~x C ~x C x = 0 Decimal Hex Binary ~0-1 FF FF ~

15 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 15

16 Unsigned Addition Operands: w bits True Sum: w+1 bits Discard Carry: w bits u + v u + v Addition Operation Carry output dropped at end of addition Valid ONLY if true sum is within w-bit range 16

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

18 Unsigned Addition Example #2: Not Valid in 8-bit unsigned range (312 is > 255) 18

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

20 Visualizing True Sum (Mathematical) Addition Integer Addition 4-bit integers u, v Compute true sum Values increase linearly with u and v Forms planar surface True Sum u v

21 Visualizing Unsigned Addition Wraps Around If true sum 2 w At most once Overflow True Sum 2 w+1 Overflow w 0 Modular Sum u v 14 21

22 Two s Complement Addition Operands: w bits True Sum: w+1 bits Discard Carry: w bits u + v u + v Signed/Unsigned adds have Identical Bit-Level Behavior Signed vs. unsigned addition in C: int s, t, u, v; s = (int) ((unsigned) u + (unsigned) v); t = u + v Will give s == t 22

23 Signed Addition Example #1: 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) Example #2: Valid in 8-bit signed range (-128 < 56 < 127) 23

24 Visualizing Signed Addition Negative Overflow 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 Positive Overflow 24

25 Multiplication Goal: Computing Product of w-bit numbers x, y Either signed or unsigned But, exact results can be bigger than w bits Unsigned: up to 2w bits Result range: 0 x * y (2 w 1) 2 = 2 2w 2 w Two s complement min (negative): Up to 2w-1 bits Result range: x * y ( 2 w 1 )*(2 w 1 1) = 2 2w w 1 Two s complement max (positive): Up to 2w bits, but only for (SMin w ) 2 Result range: x * y ( 2 w 1 ) 2 = 2 2w 2 So, maintaining exact results would need to keep expanding word size with each product computed is done in software, if needed e.g., by arbitrary precision arithmetic packages 25

26 Unsigned Multiplication in C Operands: w bits * u v True Product: 2*w bits u v Discard w bits: w bits Standard Multiplication Function Ignores high order w bits Implements Modular Arithmetic machine(u v) = true(u v) mod 2 w 26

27 Signed Multiplication in C Operands: w bits True Product: 2*w bits u v * u v Discard w bits: w bits Standard Multiplication Function Ignores high order w bits Some of which are different for signed vs. unsigned multiplication Lower bits are the same 27

28 True Binary Multiplication Multiply positive integers using the same place-value algorithm you learned in grade school X

29 True Binary Multiplication Multiply positive integers using the same place-value algorithm you learned in grade school X X

30 True Binary Multiplication Multiply positive integers using the same place-value algorithm you learned in grade school X X

31 True Binary Multiplication Multiply positive integers using the same place-value algorithm you learned in grade school X X

32 True Binary Multiplication Multiply positive integers using the same place-value algorithm you learned in grade school X X

33 True Binary Multiplication Multiply positive integers using the same place-value algorithm you learned in grade school X X

34 True Binary Multiplication Multiply positive integers using the same place-value algorithm you learned in grade school X X

35 Power-of-2 Multiply with Shift Consider: 6 10 * 2 10 = X

36 Power-of-2 Multiply with Shift Consider: 6 10 * 2 10 = X

37 Power-of-2 Multiply with Shift Consider: 6 10 * 2 10 = X Multiplying by two always shifts the input bit pattern by one to the left. That is: (6 10 * 2 10 ) == ( << 1) More generally- multiplying by 2 k always shifts the input by k to the left: (x 10 * 2 k ) == (x 2 << k) 37

38 Power-of-2 Multiply with Shift Operation u << k gives u * 2 k Both signed and unsigned Operands: w bits True Product: w+k bits Discard k bits: w bits u 2 k Examples u << 3 == u * 8 (u << 5) (u << 3) == u * 24 Most machines shift and add faster than multiply Compiler generates this code automatically * u 2 k k

39 Unsigned Power-of-2 Divide with Shift Quotient of Unsigned by Power of 2 u >> k gives u / 2 k Uses logical shift k u Operands: / 2 k Division: Result: u / 2 k u / 2 k Binary Point Division Computed Hex Binary x B 6D x >> D B x >> B x >> B

40 Signed Power-of-2 Divide with Shift Quotient of Signed by Power of 2 x >> k gives x / 2 k Uses arithmetic shift Rounds wrong direction when u < 0 Operands: Division: Result: / x 2 k x / 2 k RoundDown(x / 2 k ) k Binary Point Division Computed Hex Binary y C y >> E y >> FC y >> FF C

41 Incorrect Power-of-2 Divide Consider: -25 / 2 We expect that -25 / 2 = -12, however: = (-25 / 2 ) becomes ( >> 1) 3. ( >> 1) = =

42 Correct Power-of-2 Divide with Biasing Quotient of Negative Number by Power of 2 Want x / 2 k (Round Toward 0) Compute as (x+2 k -1)/ 2 k In C: (x + (1<<k)-1) >> k Biases dividend toward 0 Dividend s low bits are zero Case 1: No rounding Dividend: Divisor: u +2 k 1 / 2 k u / 2 k k Binary Point Biasing has no effect 42

43 Biasing without changing result Consider: -20 / 4 (answer should be -5) Without bias: = (-20 / 4 ) becomes ( >> 2) 3. ( >> 2) = = -5 With bias: = (-23 / 4 ) becomes ( >> 2) 3. ( >> 2) = = -5 43

44 Correct Power-of-2 Divide (Cont.) Case 2: Rounding Dividend: k 1 x +2 k Nonzero low bits Incremented by 1 Binary Point Divisor: 2 k / x / 2 k Biasing adds 1 to final result Incremented by 1 44

45 Biasing that does change the result Consider: -21 / 4 (answer should be -5) Without bias: = (-21 / 4 ) becomes ( >> 2) 3. ( >> 2) = = -6 (incorrect!) With bias: = (-18 / 4 ) becomes ( >> 2) 3. ( >> 2) = = -5 45

46 Biasing that does change the result Consider: -21 / 4 (answer should be -5) Without bias: = (-21 / 4 ) becomes ( >> 2) 3. ( >> 2) = = -6 (incorrect!) With bias: Recall- lowest order bit has value 1! = (-18 / 4 ) becomes ( >> 2) 3. ( >> 2) = = -5 46

47 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 47

### Arithmetic and Bitwise Operations on Binary Data

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 1 Boolean Algebra Developed by

### Manipulating Integers

Manipulating Integers Jinkyu Jeong (jinkyu@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu)

### BITS, BYTES, AND INTEGERS

BITS, BYTES, AND INTEGERS CS 045 Computer Organization and Architecture Prof. Donald J. Patterson Adapted from Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition ORIGINS

### Systems Programming and Computer Architecture ( )

Systems Group Department of Computer Science ETH Zürich Systems Programming and Computer Architecture (252-0061-00) Timothy Roscoe Herbstsemester 2016 1 3: Integers in C Computer Architecture and Systems

### Lecture 5-6: Bits, Bytes, and Integers

CSCI-UA.0201-003 Computer Systems Organization Lecture 5-6: Bits, Bytes, and Integers Mohamed Zahran (aka Z) mzahran@cs.nyu.edu http://www.mzahran.com Slides adapted from: Jinyang Li Bryant and O Hallaron

### Bits, Bytes, and Integers

Bits, Bytes, and Integers with contributions from Dr. Bin Ren, College of William & Mary 1 Bits, Bytes, and Integers Representing information as bits Bit-level manipulations Integers Representation: unsigned

### Representing and Manipulating Integers. Jo, Heeseung

Representing and Manipulating Integers Jo, Heeseung Unsigned Integers Encoding unsigned integers B [ bw 1, bw 2,..., b0 ] x = 0000 0111 1101 0011 2 D( B) w 1 b i i 0 2 i D(x) = 2 10 + 2 9 + 2 8 + 2 7 +

### Bits, Bytes, and Integers Part 2

Bits, Bytes, and Integers Part 2 15-213: Introduction to Computer Systems 3 rd Lecture, Jan. 23, 2018 Instructors: Franz Franchetti, Seth Copen Goldstein, Brian Railing 1 First Assignment: Data Lab Due:

CS 33 Data Representation, Part 1 CS33 Intro to Computer Systems VII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved. Number Representation Hindu-Arabic numerals developed by Hindus starting in

### Jin-Soo Kim Systems Software & Architecture Lab. Seoul National University. Integers. Spring 2019

Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Architecture Lab. Seoul National University Integers Spring 2019 4190.308: Computer Architecture Spring 2019 Jin-Soo Kim (jinsoo.kim@snu.ac.kr) 2 A

### Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

Carnegie Mellon 1 Bits, Bytes and Integers Part 1 15-213/18-213/15-513: Introduction to Computer Systems 2 nd Lecture, Aug. 31, 2017 Today s Instructor: Randy Bryant 2 Announcements Recitations are on

### Integer Encoding and Manipulation

CSE 2421: Systems I Low-Level Programming and Computer Organization Integer Encoding and Manipulation Presentation D Study: Bryant Chapter 2.1 2.3 Gojko Babić 01-24-2018 Unsigned & Signed Integer Encoding

### ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

ICS 2008 Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2 Data Representations Sizes of C Objects (in Bytes) C Data Type Compaq Alpha Typical 32-bit Intel IA32 int 4 4 4 long int 8 4 4

### CS140 Lecture 08: Data Representation: Bits and Ints. John Magee 13 February 2017

CS140 Lecture 08: Data Representation: Bits and Ints John Magee 13 February 2017 Material From Computer Systems: A Programmer's Perspective, 3/E (CS:APP3e) Randal E. Bryant and David R. O'Hallaron, Carnegie

### Integers. Dr. Steve Goddard Giving credit where credit is due

CSCE 23J Computer Organization Integers Dr. Steve Goddard goddard@cse.unl.edu http://cse.unl.edu/~goddard/courses/csce23j Giving credit where credit is due Most of slides for this lecture are based on

### Bits, Bytes and Integers

Bits, Bytes and Integers Computer Systems Organization (Spring 2016) CSCI-UA 201, Section 2 Instructor: Joanna Klukowska Slides adapted from Randal E. Bryant and David R. O Hallaron (CMU) Mohamed Zahran

### CS 270: Computer Organization. Integer Arithmetic Operations

CS 270: Computer Organization Integer Arithmetic Operations Instructor: Professor Stephen P. Carl Unsigned Addition Operands: w bits True Sum: w+1 bits u + v u + v Discard Carry UAdd w (u, v) Standard

### Bits, Bytes, and Integers August 26, 2009

15-213 The Class That Gives CMU Its Zip! Bits, Bytes, and Integers August 26, 2009 Topics Representing information as bits Bit-level manipulations Boolean algebra Expressing in C Representations of Integers

### Systems 1. Integers. Unsigned & Twoʼs complement. Addition, negation, multiplication

Systems 1 Integers Topics Numeric Encodings Unsigned & Twoʼs complement Programming Implications C promotion rules Basic operations Addition, negation, multiplication Programming Implications Consequences

### Integers Sep 3, 2002

15-213 The course that gives CMU its Zip! Topics Numeric Encodings Unsigned & Two s complement Programming Implications C promotion rules Basic operations Integers Sep 3, 2002 Addition, negation, multiplication

### But first, encode deck of cards. Integer Representation. Two possible representations. Two better representations WELLESLEY CS 240 9/8/15

Integer Representation Representation of integers: unsigned and signed Sign extension Arithmetic and shifting Casting But first, encode deck of cards. cards in suits How do we encode suits, face cards?

### Bits and Bytes: Data Presentation Mohamed Zahran (aka Z)

CSCI-UA.0201 Computer Systems Organization Bits and Bytes: Data Presentation Mohamed Zahran (aka Z) mzahran@cs.nyu.edu http://www.mzahran.com Some slides adapted and modified from: Clark Barrett Jinyang

### Bits and Bytes. Why bits? Representing information as bits Binary/Hexadecimal Byte representations» numbers» characters and strings» Instructions

Bits and Bytes Topics Why bits? Representing information as bits Binary/Hexadecimal Byte representations» numbers» characters and strings» Instructions Bit-level manipulations Boolean algebra Expressing

### Why Don t Computers Use Base 10? Lecture 2 Bits and Bytes. Binary Representations. Byte-Oriented Memory Organization. Base 10 Number Representation

Lecture 2 Bits and Bytes Topics Why bits? Representing information as bits Binary/Hexadecimal Byte representations» numbers» characters and strings» Instructions Bit-level manipulations Boolean algebra

### Bits, Bytes and Integers Part 1

Bits, Bytes and Integers Part 1 15-213/18-213/15-513: Introduction to Computer Systems 2 nd Lecture, Jan. 18, 2018 Instructors: Franz Franchetti Seth Copen Goldstein Brian Railing 1 Announcements Waitlist

### Integers. Today. Next time. ! Numeric Encodings! Programming Implications! Basic operations. ! Floats

Integers Today! Numeric Encodings! Programming Implications! Basic operations! Programming Implications Next time! Floats Fabián E. Bustamante, 2007 Integers in C! C supports several integral data types

### Page 1. Where Have We Been? Chapter 2 Representing and Manipulating Information. Why Don t Computers Use Base 10?

Where Have We Been? Class Introduction Great Realities of Computing Int s are not Integers, Float s are not Reals You must know assembly Memory Matters Performance! Asymptotic Complexity It s more than

### Why Don t Computers Use Base 10? Lecture 2 Bits and Bytes. Binary Representations. Byte-Oriented Memory Organization. Base 10 Number Representation

Lecture 2 Bits and Bytes Topics! Why bits?! Representing information as bits " Binary/Hexadecimal " Byte representations» numbers» characters and strings» Instructions! Bit-level manipulations " Boolean

CS 33 Data Representation, Part 2 CS33 Intro to Computer Systems VII 1 Copyright 2018 Thomas W. Doeppner. All rights reserved. Signed Integers Two s complement b w-1 = 0 Þ non-negative number value = b

### CSCI2467: Systems Programming Concepts

CSCI2467: Systems Programming Concepts Slideset 2: Information as Data (CS:APP Chap. 2) Instructor: M. Toups Spring 2018 Course updates datalab out today! - due after Mardi gras... - do not wait until

### Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2

Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2 Instructor: Rob Bergen Applied Computer Science University of Winnipeg Announcements Website is up

### Bits and Bytes January 13, 2005

15-213 The Class That Gives CMU Its Zip! Topics Bits and Bytes January 13, 25 Why bits? Representing information as bits Binary / Hexadecimal Byte representations» Numbers» Characters and strings» Instructions

### Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators JAVA Standard Edition Java - Basic Operators Java provides a rich set of operators to manipulate variables.

### Computer Systems CEN591(502) Fall 2011

Computer Systems CEN591(502) Fall 2011 Sandeep K. S. Gupta Arizona State University 4 th lecture Data representation in computer systems (Slides adapted from CSAPP book) Announcements Programming assignment

### Representa7on of integers: unsigned and signed Conversion, cas7ng Expanding, trunca7ng Addi7on, nega7on, mul7plica7on, shiaing

Today s Topics Representa7on of integers: unsigned and signed Conversion, cas7ng Expanding, trunca7ng Addi7on, nega7on, mul7plica7on, shiaing CSE351 Inaugural Edi7on Spring 2010 1 Encoding Integers C short

### Bits, Bytes, and Integers

Bits, Bytes, and Integers B&O Readings: 2.1-2.3 CSE 361: Introduc=on to Systems So@ware Instructor: I- Ting Angelina Le e Note: these slides were originally created by Markus Püschel at Carnegie Mellon

### Topics of this Slideset. CS429: Computer Organization and Architecture. Encoding Integers: Unsigned. C Puzzles. Integers

Topics of this Slideset CS429: Computer Organization and Architecture Dr. Bill Young Department of Computer Science University of Texas at Austin Last updated: July 5, 2018 at 11:55 Numeric Encodings:

### CS429: Computer Organization and Architecture

CS429: Computer Organization and Architecture Dr. Bill Young Department of Computer Science University of Texas at Austin Last updated: July 5, 2018 at 11:55 CS429 Slideset 3: 1 Topics of this Slideset

### 2.1. Unit 2. Integer Operations (Arithmetic, Overflow, Bitwise Logic, Shifting)

2.1 Unit 2 Integer Operations (Arithmetic, Overflow, Bitwise Logic, Shifting) 2.2 Skills & Outcomes You should know and be able to apply the following skills with confidence Perform addition & subtraction

CS 33 Data Representation, Part 2 CS33 Intro to Computer Systems VIII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved. Numeric Ranges Unsigned Values UMin = 0 000 0 UMax = 2 w 1 111 1 Two s Complement

### Foundations of Computer Systems

18-600 Foundations of Computer Systems Lecture 3: Bits, Bytes, and Integers September 6, 2017 Required Reading Assignment: Chapter 2 of CS:APP (3 rd edition) by Randy Bryant & Dave O Hallaron Assignments

### Data III & Integers I

Data III & Integers I CSE 351 Spring 2017 Instructor: Ruth Anderson Teaching Assistants: Dylan Johnson Kevin Bi Linxing Preston Jiang Cody Ohlsen Yufang Sun Joshua Curtis Administrivia Everyone has VM

### Integers II. CSE 351 Autumn Instructor: Justin Hsia

Integers II CSE 351 Autumn 2016 Instructor: Justin Hsia Teaching Assistants: Chris Ma Hunter Zahn John Kaltenbach Kevin Bi Sachin Mehta Suraj Bhat Thomas Neuman Waylon Huang Xi Liu Yufang Sun http://xkcd.com/571/

### CSC 2400: Computer Systems. Bit- Level vs. Logical Opera<ons. Bit- Level Operators. Common to C and Java &,, ~, ^, <<, >>

CSC 2400: Computer Systems Bit- Level vs. Logical Opera

### Data III & Integers I

Data III & Integers I CSE 351 Autumn 2018 Instructor: Justin Hsia Teaching Assistants: Akshat Aggarwal An Wang Andrew Hu Brian Dai Britt Henderson James Shin Kevin Bi Kory Watson Riley Germundson Sophie

### Data III & Integers I

Data III & Integers I CSE 351 Autumn 2016 Instructor: Justin Hsia Teaching Assistants: Chris Ma Hunter Zahn John Kaltenbach Kevin Bi Sachin Mehta Suraj Bhat Thomas Neuman Waylon Huang Xi Liu Yufang Sun

### Integers II. CSE 351 Autumn Instructor: Justin Hsia

Integers II CSE 351 Autumn 2017 Instructor: Justin Hsia Teaching Assistants: Lucas Wotton Michael Zhang Parker DeWilde Ryan Wong Sam Gehman Sam Wolfson Savanna Yee Vinny Palaniappan http://xkcd.com/557/

### Chapter 4. Operations on Data

Chapter 4 Operations on Data 1 OBJECTIVES After reading this chapter, the reader should be able to: List the three categories of operations performed on data. Perform unary and binary logic operations

### Integers II. CSE 351 Autumn 2018

Integers II CSE 351 Autumn 2018 Instructor: Teaching Assistants: Justin Hsia Akshat Aggarwal An Wang Andrew Hu Brian Dai Britt Henderson James Shin Kevin Bi Kory Watson Riley Germundson Sophie Tian Teagan

### Floating Point Numbers

Floating Point Numbers Computer Systems Organization (Spring 2016) CSCI-UA 201, Section 2 Instructor: Joanna Klukowska Slides adapted from Randal E. Bryant and David R. O Hallaron (CMU) Mohamed Zahran

### Floating Point Numbers

Floating Point Numbers Computer Systems Organization (Spring 2016) CSCI-UA 201, Section 2 Fractions in Binary Instructor: Joanna Klukowska Slides adapted from Randal E. Bryant and David R. O Hallaron (CMU)

### Data Representation Floating Point

Data Representation Floating Point CSCI 2400 / ECE 3217: Computer Architecture Instructor: David Ferry Slides adapted from Bryant & O Hallaron s slides via Jason Fritts Today: Floating Point Background:

### COMP Overview of Tutorial #2

COMP 1402 Winter 2008 Tutorial #2 Overview of Tutorial #2 Number representation basics Binary conversions Octal conversions Hexadecimal conversions Signed numbers (signed magnitude, one s and two s complement,

### CHW 261: Logic Design

CHW 261: Logic Design Instructors: Prof. Hala Zayed Dr. Ahmed Shalaby http://www.bu.edu.eg/staff/halazayed14 http://bu.edu.eg/staff/ahmedshalaby14# Slide 1 Slide 2 Slide 3 Digital Fundamentals CHAPTER

### CS 31: Introduction to Computer Systems. 03: Binary Arithmetic January 29

CS 31: Introduction to Computer Systems 03: Binary Arithmetic January 29 WiCS! Swarthmore Women in Computer Science Slide 2 Today Binary Arithmetic Unsigned addition Subtraction Representation Signed magnitude

### Data Representation Floating Point

Data Representation Floating Point CSCI 2400 / ECE 3217: Computer Architecture Instructor: David Ferry Slides adapted from Bryant & O Hallaron s slides via Jason Fritts Today: Floating Point Background:

### Binary Representations and Arithmetic

Binary Representations and Arithmetic 9--26 Common number systems. Base : decimal Base 2: binary Base 6: hexadecimal (memory addresses) Base 8: octal (obsolete computer systems) Base 64 (email attachments,

### JAVA OPERATORS GENERAL

JAVA OPERATORS GENERAL Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators

### Floating-Point Data Representation and Manipulation 198:231 Introduction to Computer Organization Lecture 3

Floating-Point Data Representation and Manipulation 198:231 Introduction to Computer Organization Instructor: Nicole Hynes nicole.hynes@rutgers.edu 1 Fixed Point Numbers Fixed point number: integer part

### COMP2611: Computer Organization. Data Representation

COMP2611: Computer Organization Comp2611 Fall 2015 2 1. Binary numbers and 2 s Complement Numbers 3 Bits: are the basis for binary number representation in digital computers What you will learn here: How

### Module 2: Computer Arithmetic

Module 2: Computer Arithmetic 1 B O O K : C O M P U T E R O R G A N I Z A T I O N A N D D E S I G N, 3 E D, D A V I D L. P A T T E R S O N A N D J O H N L. H A N N E S S Y, M O R G A N K A U F M A N N

### CS107, Lecture 3 Bits and Bytes; Bitwise Operators

CS107, Lecture 3 Bits and Bytes; Bitwise Operators reading: Bryant & O Hallaron, Ch. 2.1 This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative Commons Attribution

### 7/25/2016. Example: Addition of Unsigned Bit Patterns. ECE 120: Introduction to Computing. Adding Two Non-Negative Patterns Can Overflow

University of Illinois at Urbana-Champaign Dept. of Electrical and Computer Engineering ECE 120: Introduction to Computing 2 s Complement Overflow and Boolean Logic Example: ddition of Unsigned Bit Patterns

### 4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

4 Operations On Data 4.1 Foundations of Computer Science Cengage Learning Objectives After studying this chapter, the student should be able to: List the three categories of operations performed on data.

### Page # CISC360. Integers Sep 11, Encoding Integers Unsigned. Encoding Example (Cont.) Topics. Twoʼs Complement. Sign Bit

Topics CISC3 Integers Sep 11, 28 Nmeric Encodings Unsigned & Twoʼs complement Programming Implications C promotion rles Basic operations Addition, negation, mltiplication Programming Implications Conseqences

### ECE260: Fundamentals of Computer Engineering

Arithmetic for Computers James Moscola Dept. of Engineering & Computer Science York College of Pennsylvania Based on Computer Organization and Design, 5th Edition by Patterson & Hennessy Arithmetic for

### Arithmetic Processing

CS/EE 5830/6830 VLSI ARCHITECTURE Chapter 1 Basic Number Representations and Arithmetic Algorithms Arithmetic Processing AP = (operands, operation, results, conditions, singularities) Operands are: Set

### Computer Organization: A Programmer's Perspective

A Programmer's Perspective Bits, Bytes, Nibbles, Words and Strings Gal A. Kaminka galk@cs.biu.ac.il Topics Why bits? Why 0/1? Basic terms: Bits, Bytes, Nibbles, Words Representing information as bits Characters

### Hardware: Logical View

Hardware: Logical View CPU Memory Bus Disks Net USB Etc. 1 Hardware: Physical View USB I/O controller Storage connections CPU Memory 2 Hardware: 351 View (version 0) instructions? Memory CPU data CPU executes

### Bitwise Data Manipulation. Bitwise operations More on integers

Bitwise Data Manipulation Bitwise operations More on integers bitwise operators ex Bitwise operators on fixed-width bit vectors. AND & OR XOR ^ NOT ~ 01101001 & 01010101 01000001 01101001 01010101 01101001

### The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators The arithmetic operators refer to the standard mathematical operators: addition, subtraction, multiplication, division and modulus. Op. Use Description + x + y adds x and y x y

### The Arithmetic Operators

The Arithmetic Operators The arithmetic operators refer to the standard mathematical operators: addition, subtraction, multiplication, division and modulus. Examples: Op. Use Description + x + y adds x

### Bits, Words, and Integers

Computer Science 52 Bits, Words, and Integers Spring Semester, 2017 In this document, we look at how bits are organized into meaningful data. In particular, we will see the details of how integers are

### 4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

4 Operations On Data 4.1 Foundations of Computer Science Cengage Learning Objectives After studying this chapter, the student should be able to: List the three categories of operations performed on data.

### Number Systems and Computer Arithmetic

Number Systems and Computer Arithmetic Counting to four billion two fingers at a time What do all those bits mean now? bits (011011011100010...01) instruction R-format I-format... integer data number text

### CS 31: Intro to Systems Binary Arithmetic. Kevin Webb Swarthmore College January 26, 2016

CS 31: Intro to Systems Binary Arithmetic Kevin Webb Swarthmore College January 26, 2016 Reading Quiz Unsigned Integers Suppose we had one byte Can represent 2 8 (256) values If unsigned (strictly non-negative):

### CS61B Lecture #14: Integers. Last modified: Wed Sep 27 15:44: CS61B: Lecture #14 1

CS61B Lecture #14: Integers Last modified: Wed Sep 27 15:44:05 2017 CS61B: Lecture #14 1 Integer Types and Literals Type Bits Signed? Literals byte 8 Yes Cast from int: (byte) 3 short 16 Yes None. Cast

### CS 261 Fall Mike Lam, Professor Integer Encodings

CS 261 Fall 2018 Mike Lam, Professor https://xkcd.com/571/ Integer Encodings Integers Topics C integer data types Unsigned encoding Signed encodings Conversions Integer data types in C99 1 byte 2 bytes

### Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Basic Operators Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators

### Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Introduction Operators are the symbols which operates on value or a variable. It tells the compiler to perform certain mathematical or logical manipulations. Can be of following categories: Unary requires

### Lecture Topics. Announcements. Today: Integer Arithmetic (P&H ) Next: continued. Consulting hours. Introduction to Sim. Milestone #1 (due 1/26)

Lecture Topics Today: Integer Arithmetic (P&H 3.1-3.4) Next: continued 1 Announcements Consulting hours Introduction to Sim Milestone #1 (due 1/26) 2 1 Overview: Integer Operations Internal representation

### Integer Representation

Integer Representation Announcements assign0 due tonight assign1 out tomorrow Labs start this week SCPD Note on ofce hours on Piazza Will get an email tonight about labs Goals for Today Introduction to

### COMP 122/L Lecture 2. Kyle Dewey

COMP 122/L Lecture 2 Kyle Dewey Outline Operations on binary values AND, OR, XOR, NOT Bit shifting (left, two forms of right) Addition Subtraction Twos complement Bitwise Operations Bitwise AND Similar

### Chapter 2 Float Point Arithmetic. Real Numbers in Decimal Notation. Real Numbers in Decimal Notation

Chapter 2 Float Point Arithmetic Topics IEEE Floating Point Standard Fractional Binary Numbers Rounding Floating Point Operations Mathematical properties Real Numbers in Decimal Notation Representation

### Lecture 8: Addition, Multiplication & Division

Lecture 8: Addition, Multiplication & Division Today s topics: Signed/Unsigned Addition Multiplication Division 1 Signed / Unsigned The hardware recognizes two formats: unsigned (corresponding to the C

### 4/8/17. Admin. Assignment 5 BINARY. David Kauchak CS 52 Spring 2017

4/8/17 Admin! Assignment 5 BINARY David Kauchak CS 52 Spring 2017 Diving into your computer Normal computer user 1 After intro CS After 5 weeks of cs52 What now One last note on CS52 memory address binary

### 9/23/15. Agenda. Goals of this Lecture. For Your Amusement. Number Systems and Number Representation. The Binary Number System

For Your Amusement Number Systems and Number Representation Jennifer Rexford Question: Why do computer programmers confuse Christmas and Halloween? Answer: Because 25 Dec = 31 Oct -- http://www.electronicsweekly.com

### Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Course Name: Advanced Java Lecture 2 Topics to be covered Variables Operators Variables -Introduction A variables can be considered as a name given to the location in memory where values are stored. One

### Page 1 CISC360. Encoding Integers Unsigned. Integers Sep 8, Numeric Ranges. Encoding Example (Cont.)

CISC3 Integers Sep, 9 Encoding Integers Unsigned w 1 BU(X) = x i i i= short int x = 1513; short int y = -1513; Twoʼs Complement w BT(X) = x w 1 w 1 + x i i i= Sign Bit Decimal Hex Binary x 1513 3B D 11111

### Numbers and Computers. Debdeep Mukhopadhyay Assistant Professor Dept of Computer Sc and Engg IIT Madras

Numbers and Computers Debdeep Mukhopadhyay Assistant Professor Dept of Computer Sc and Engg IIT Madras 1 Think of a number between 1 and 15 8 9 10 11 12 13 14 15 4 5 6 7 12 13 14 15 2 3 6 7 10 11 14 15

### GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

http://www.tutorialspoint.com/go/go_operators.htm GO - OPERATORS Copyright tutorialspoint.com An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.

### Lecture 9. INC and DEC. INC/DEC Examples ADD. Arithmetic Operations Overflow Multiply and Divide

Lecture 9 INC and DEC Arithmetic Operations Overflow Multiply and Divide INC adds one to a single operand DEC decrements one from a single operand INC destination DEC destination where destination can

### Semester Transition Point. EE 109 Unit 11 Binary Arithmetic. Binary Arithmetic ARITHMETIC

1 2 Semester Transition Point EE 109 Unit 11 Binary Arithmetic At this point we are going to start to transition in our class to look more at the hardware organization and the low-level software that is

### COMPUTER ARITHMETIC (Part 1)

Eastern Mediterranean University School of Computing and Technology ITEC255 Computer Organization & Architecture COMPUTER ARITHMETIC (Part 1) Introduction The two principal concerns for computer arithmetic

### Number Systems Standard positional representation of numbers: An unsigned number with whole and fraction portions is represented as:

N Number Systems Standard positional representation of numbers: An unsigned number with whole and fraction portions is represented as: a n a a a The value of this number is given by: = a n Ka a a a a a

### Inf2C - Computer Systems Lecture 2 Data Representation

Inf2C - Computer Systems Lecture 2 Data Representation Boris Grot School of Informatics University of Edinburgh Last lecture Moore s law Types of computer systems Computer components Computer system stack

### Chapter 5 : Computer Arithmetic

Chapter 5 Computer Arithmetic Integer Representation: (Fixedpoint representation): An eight bit word can be represented the numbers from zero to 255 including = 1 = 1 11111111 = 255 In general if an nbit

### Arithmetic for Computers

MIPS Arithmetic Instructions Cptr280 Dr Curtis Nelson Arithmetic for Computers Operations on integers Addition and subtraction; Multiplication and division; Dealing with overflow; Signed vs. unsigned numbers.