Manipulating Integers

Similar documents
Representing and Manipulating Integers. Jo, Heeseung

CS 270: Computer Organization. Integer Arithmetic Operations

Systems Programming and Computer Architecture ( )

Arithmetic and Bitwise Operations on Binary Data

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

Bits, Bytes, and Integers August 26, 2009

Arithmetic and Bitwise Operations on Binary Data

Lecture 5-6: Bits, Bytes, and Integers

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

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

Bits, Bytes, and Integers Part 2

CS 33. Data Representation, Part 2. CS33 Intro to Computer Systems VII 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

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

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

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

Integers Sep 3, 2002

Bits, Bytes, and Integers

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

CS 33. Data Representation, Part 2. CS33 Intro to Computer Systems VIII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

BITS, BYTES, AND INTEGERS

Integer Encoding and Manipulation

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

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

CS429: Computer Organization and Architecture

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

Computer Systems CEN591(502) Fall 2011

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

Foundations of Computer Systems

CS 33. Data Representation, Part 1. CS33 Intro to Computer Systems VII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Bits, Bytes and Integers

Representing Integers

Representing and Manipulating Floating Points

Floating Point Arithmetic

Bits, Bytes and Integers Part 1

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

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

Bits, Bytes, and Integers

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Integers II. CSE 351 Autumn Instructor: Justin Hsia

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

CSCI2467: Systems Programming Concepts

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

Chapter 4. Operations on Data

Computer Organization: A Programmer's Perspective

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

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

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

Integers II. CSE 351 Autumn 2018

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

Integer Arithmetic. CS 347 Lecture 03. January 20, 1998

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

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

Credits and Disclaimers

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

Assembly II: Control Flow

Byte Ordering. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

AGENDA Binary Operations CS 3330 Samira Khan

Bits and Bytes January 13, 2005

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

Integer Arithmetic. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

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

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Adding Binary Integers. Part 4. Negative Binary. Integers. Adding Base 10 Numbers. Adding Binary Example = 10. Arithmetic Logic Unit

Integer Representation

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Bits, Bytes, and Integers

Credits and Disclaimers

Bitwise Data Manipulation. Bitwise operations More on integers

CS 3843 Final Exam Fall 2012

EE 109 Unit 6 Binary Arithmetic

CSE 351 Midterm - Winter 2017

Programming in C++ 5. Integral data types

Arithmetic Operations

Floating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754

UW CSE 351, Winter 2013 Midterm Exam

September, Saeid Nooshabadi. Review: Floating Point Representation COMP Single Precision and Double Precision

C Puzzles The course that gives CMU its Zip! Integer Arithmetic Operations Jan. 25, Unsigned Addition. Visualizing Integer Addition

Homework 3. Assigned on 02/15 Due time: midnight on 02/21 (1 WEEK only!) B.2 B.11 B.14 (hint: use multiplexors) CSCI 402: Computer Architectures

Advanced Computer Architecture-CS501

Fixed-Point Math and Other Optimizations

Module 2: Computer Arithmetic

Computer Organization & Systems Exam I Example Questions

SIGNED AND UNSIGNED SYSTEMS

CS61B Lecture #14: Integers

Data III & Integers I

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Adding Binary Integers. Part 5. Adding Base 10 Numbers. Adding 2's Complement. Adding Binary Example = 10. Arithmetic Logic Unit

CSC 8400: Computer Systems. Machine-Level Representation of Programs

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

Chapter Two MIPS Arithmetic

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

Integer Representation

Floating Point Arithmetic. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

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

COMP 122/L Lecture 2. Kyle Dewey

Systems I. Floating Point. Topics IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties

Computer Architecture and IC Design Lab. Chapter 3 Part 2 Arithmetic for Computers Floating Point

Floating Point January 24, 2008

MIPS Integer ALU Requirements

Transcription:

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)

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) ~0x41 à 0xBE ~01000001 2 à 10111110 2 ~0x00 à 0xFF ~00000000 2 à 11111111 2 0x69 & 0x55 à 0x41 01101001 2 & 01010101 2 à 01000001 2 0x69 0x55 à 0x7D 01101001 2 & 01010101 2 à 01111101 2 0x69 ^ 0x55 à 0x3C 01101001 2 & 01010101 2 à 00111100 2 SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 2

Logic Operations in C &&,,! 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 if (p && *p) // avoids null pointer access SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 3

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 MSB on right Useful with two s complement integer representation Undefined if y < 0 or y ³ word size Argument x << 3 Log. >> 2 Arith. >> 2 Argument x << 3 Log. >> 2 Arith. >> 2 01100010 00010000 00011000 00011000 10100010 00010000 00101000 11101000 SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 4

Addition (1) Integer addition example 4-bit integers u, v Compute true sum True sum requires one more bit ( carry ) Values increase linearly with u and v Forms planar surface SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 5

Addition (2) Unsigned addition Ignores carry output Wraps around If true sum 2 w At most once True Sum 2 w+1 Overflow 2 w 0 Unsigned addition SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 6

Addition (3) Signed addition Drop off MSB Treat remaining bits as two s complement integers True Sum +2 w Positive overflow Negative overflow +2 w 1 +2 w 1 0 0 2 w 1 2 w Negative overflow 2 w 1 Signed addition Positive overflow SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 7

Addition (4) Signed addition in C Ignores carry output The low order w bits are identical to unsigned addition Examples for w = 3 Mode x y x + y Truncated x + y Unsigned 4 [100] 7 [0111] 7 [111] Two s comp. -4 [100] -1 [1111] -1 [111] Unsigned 4 [100] 7 [111] 11 [1011] Two s comp. -4 [100] -1 [111] -5 [1011] Unsigned 6 [0110] 6 [110] Two s comp. 6 [0110] -2 [110] SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 8

Multiplication (1) Ranges of (x * y) Unsigned: up to 2w bits 0 x * y (2 w 1) 2 = 2 2w 2 w+1 + 1 Two s complement min: up to 2w-1 bits x * y ( 2 w 1 )*(2 w 1 1) = 2 2w 2 + 2 w 1 Two s complement max: up to 2w bits (only for TMin 2 ) x * y ( 2 w 1 ) 2 = 2 2w 2 Maintaining exact results Would need to keep expanding word size with each product computed Done in software by arbitrary precision arithmetic packages SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 9

Multiplication (2) Unsigned multiplication in C Ignores high order w bits Implements modular arithmetic UMult ( u, v) = u v mod 2 w w Operands: w bits * u v True Product: 2*w bits u v Discard w bits: w bits UMult w (u, v) SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 10

Multiplication (3) Signed multiplication in C Ignores high order w bits The low-order w bits are identical to unsigned multiplication Examples for w = 3 Mode x y x y Truncated x y Unsigned 5 [101] 15 [001111] 7 [111] Two s comp. -3 [101] -9 [110111] -1 [111] Unsigned 4 [100] 7 [111] 28 [011100] 4 [100] Two s comp. -4 [100] -1 [111] 4 [000100] -4 [100] Unsigned 9 [001001] 1 [001] Two s comp. 9 [001001] 1 [001] SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 11

Multiplication (4) Power-of-2 multiply with shift u << k gives u * 2 k e.g. u << 3 == u * 8, (u << 5) (u << 3) == u * 24 Both signed and unsigned Most machines shift and add faster than multiply Operands: w bits True Product: w+k bits u 2 k Discard k bits: w bits UMult w (u, 2 k ) TMult w (u, 2 k ) * u 2 k k 0 0 1 0 0 0 0 0 0 0 0 0 SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 12

Multiplication (5) Compiled multiplication code C compiler automatically generates shift/add code when multiplying by constant C Function int mul12 (int x) { return x * 12; } Compiled Arithmetic Operations leal (%eax, %eax, 2), %eax ; t ß x + x * 2 sall $2, %eax ; return t << 2 SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 13

Division (1) Unsigned power-of-2 divide with shift u >> k gives ë u / 2 k û Uses logical shift Operands: / u 2 k k 0 0 1 0 0 0 Binary Point Division: u / 2 k. Result: ë u / 2 k û Expression Division Result Hex Binary x 15213 15213 3B 6D 00111011 01101101 x >> 1 7606.5 7606 1D B6 00011101 10110110 x >> 4 950.8125 950 03 B6 00000011 10110110 x >> 8 59.4257813 59 00 3B 00000000 00111011 SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 14

Division (2) Compiled unsigned division code Uses logical shift for unsigned Logical shift written as >>> in Java C Function unsigned udiv8 (unsigned x) { return x / 8; } Compiled Arithmetic Operations shrl $3, %eax ; return t >> 3 SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 15

Division (3) Signed power-of-2 divide with shift x >> k gives ë x / 2 k û Uses arithmetic shift (rounds wrong direction if x < 0) Operands: Division: Result: / x 2 k x / 2 k RoundDown(x / 2 k ) k 0 0 1 0 0 0 SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 16. Binary Point Expression Division Result Hex Binary y -15213-15213 C4 93 11000100 10010011 y >> 1-7606.5-7607 E2 49 11100010 01001001 y >> 4-950.8125-951 FC 49 11111100 01001001 y >> 8-59.4257813-60 FF C4 11111111 11000100

Division (4) Correct power-of-2 divide Want é x / 2 k ù (Round Toward 0) when x < 0 Compute as é (x + 2 k 1) / 2 k ù In C: (x + (1 << k) 1) >> k Biases dividend toward 0 Case 1: No rounding Biasing has no effect Dividend: Divisor: / x 2 k 1 0 0 0 +2 k 1 0 0 0 1 1 1 1 1 1 1 0 0 1 0 0 0 Binary Point é x / 2 k ù 1 1 1 1. 1 1 1 SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 17

Division (5) Case 2: Rounding Biasing adds 1 to final result Dividend: x k 1 +2 k 1 0 0 0 1 1 1 1 Incremented by 1 Binary Point Divisor: / 2 k 0 0 1 0 0 0 é x / 2 k ù 1 1 1 1. Incremented by 1 SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 18

Division (6) Compiled signed division code Uses arithmetic shift for signed Arithmetic shift written as >> in Java Compiled Arithmetic Operations testl %eax, %eax js L4 L3: sarl $3, %eax ret L4: addl $7, %eax jmp L3 C Function int idiv8 (int x) { return x / 8; } Explanation if (x < 0) x += 7; return x >> 3; SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 19