Representing and Manipulating Integers Part I

Similar documents
Representing Integers. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Representing Integers

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

Representing and Manipulating Integers. Jo, Heeseung

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

Bits, Bytes and Integers Part 1

Bits, Bytes, and Integers

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

Bits, Bytes and Integers

Lecture 5-6: Bits, Bytes, and Integers

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

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

Bits, Bytes, and Integers

Digital Systems. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

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

Integers Sep 3, 2002

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

Bits, Bytes, and Integers August 26, 2009

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

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

Computer Systems CEN591(502) Fall 2011

Systems Programming and Computer Architecture ( )

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

CS429: Computer Organization and Architecture

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

Integer Encoding and Manipulation

Arithmetic Operators. Portability: Printing Numbers

Integers II. CSE 351 Autumn Instructor: Justin Hsia

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

Computer Organization: A Programmer's Perspective

Integers II. CSE 351 Autumn Instructor: Justin Hsia

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

Integers II. CSE 351 Autumn 2018

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

BITS, BYTES, AND INTEGERS

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

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

Bits and Bytes January 13, 2005

Bits, Bytes, and Integers Part 2

Arithmetic and Bitwise Operations on Binary Data

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

Bitwise Data Manipulation. Bitwise operations More on integers

Foundations of Computer Systems

Arithmetic and Bitwise Operations on Binary Data

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

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. Bits, Bytes, and Integers. The Decimal System and Bases. Everything is bits. Converting from Decimal to Binary

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

Byte Ordering. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

The Design of C: A Rational Reconstruction

The Design of C: A Rational Reconstruction"

CSCI2467: Systems Programming Concepts

Integer Representation

Hardware: Logical View

Goals for this Week. CSC 2400: Computer Systems. Bits, Bytes and Data Types. Binary number system. Finite representations of binary integers

Few reminders and demos

The Design of C: A Rational Reconstruction" Jennifer Rexford!

Simple Data Types in C. Alan L. Cox

Integer Representation Floating point Representation Other data types

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

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

1.3b Type Conversion

File Handling in C. EECS 2031 Fall October 27, 2014

CS107, Lecture 2 Bits and Bytes; Integer Representations

CS 107 Lecture 3: Integer Representations

Programming. Elementary Concepts

REMEMBER TO REGISTER FOR THE EXAM.

EE292: Fundamentals of ECE

Binary Values. CSE 410 Lecture 02

SIGNED AND UNSIGNED SYSTEMS

Internal representation. Bitwise operators

Internal representation. Bitwise operators

Types, Operators and Expressions

The Design of C: A Rational Reconstruction

Types, Operators and Expressions

Internal representation. Bitwise operators

Chapter 7. Basic Types

C Puzzles! Taken from old exams. Integers Sep 3, Encoding Integers Unsigned. Encoding Example (Cont.) The course that gives CMU its Zip!

Topics of this Slideset. CS429: Computer Organization and Architecture. It s Bits All the Way Down. Why Binary? Why Not Decimal?

CS429: Computer Organization and Architecture

UNIT 7A Data Representation: Numbers and Text. Digital Data

1.1. Unit 1. Integer Representation

CS367 Test 1 Review Guide

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Data Types. Data Types. Integer Types. Signed Integers

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

Programming refresher and intro to C programming

THE FUNDAMENTAL DATA TYPES

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

Computer Systems Programming. Practice Midterm. Name:

CS Programming In C

Computer Organization & Systems Exam I Example Questions

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Computer System and programming in C

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

CS & IT Conversions. Magnitude 10,000 1,

Number Systems, Scalar Types, and Input and Output

EECS2031. Modifiers. Data Types. Lecture 2 Data types. signed (unsigned) int long int long long int int may be omitted sizeof()

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Reserved Words and Identifiers

Transcription:

Representing and Manipulating Integers Part I Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu

Introduction The advent of the digital age Analog vs. digital? Compact Disc (CD) MP3 44.1 KHz, 16-bit, 2-channel A digital audio encoding with lossy data compression 2

Representing Information Information = Bits + Context Computers manipulate representations of things. Things are represented as binary digits. What can you represent with N bits? 2 N things Numbers, characters, pixels, positions, source code, executable files, machine instructions, Depends on what operations you do on them. 01110011 01101011 01101011 01110101 01110011 01100101 01101101 01101001 (char) (int) (double) s k k u s e m i 1969974131 1768777075 7.03168990329170808178 x 10 199 3

Binary Representations Why not base 10 representation? Easy to store with bistable elements Straightforward implementation of arithmetic functions Reliably transmitted on noisy and inaccurate wires Electronic implementation 3.3V 2.8V 0.5V 0.0V 0 1 0 4

Encoding Byte Values Byte = 8 bits Binary: 00000000 2 to 11111111 2 Octal: 000 8 to 377 8 An integer constant that begins with 0 is an octal number in C Decimal: 0 10 to 255 10 First digit must not be 0 in C Hexadecimal: 00 16 to FF 16 Base 16 number representation Use characters 0 to 9 and A to F Write FA1D37B 16 in C as 0xFA1D37B or 0xfa1d37b 0 0 0000 1 1 0001 2 2 0010 3 3 0011 4 4 0100 5 5 0101 6 6 0110 7 7 0111 8 8 1000 9 9 1001 A 10 1010 B 11 1011 C 12 1100 D 13 1101 E 14 1110 F 15 1111 5

Boolean Algebra (1) Developed by George Boole in 1849 Algebraic representation of logic And Not Encode True as 1 and False as 0 A&B = 1 when both A=1 and B=1 & 0 1 0 0 0 1 0 1 ~A = 1 when A=0 ~ 0 1 1 0 Or A B = 1 when either A=1 or B=1 0 1 0 0 1 1 1 1 Exclusive-Or (Xor) A^B = 1 when either A=1 or B=1, but not both ^ 0 1 0 0 1 1 1 0 6

Boolean Algebra (2) 0 0 1 1 0 1 0 1 X Y 0 0 0 0 Constant 0 0 0 0 1 X & Y ; AND 0 0 1 0 ~ (X Y) 0 0 1 1 X 0 1 0 0 ~ (Y X) 0 1 0 1 Y 0 1 1 0 X ^ Y ; XOR 0 1 1 1 X Y ; OR 1 0 0 0 ~ (X Y) ; NOR 1 0 0 1 ~ (X ^ Y) ; X-NOR 1 0 1 0 ~ Y 1 0 1 1 Y X 1 1 0 0 ~ X 1 1 0 1 X Y 1 1 1 0 ~ (X & Y) ; NAND 1 1 1 1 Constant 1 X Y f f (X, Y) Basic operations: AND(&), OR( ), NOT(~) X ^ Y = (X & ~Y) (~X & Y) XY = ~X Y A complete set: NAND = ~ (X & Y) 7

Unsigned Integers Encoding unsigned integers B [ bw 1, bw2,..., b0 ] x = 0000 0111 1101 0011 2 D( B) w 1 b i i0 2 i D(x) = 2 10 + 2 9 + 2 8 + 2 7 + 2 6 + 2 4 + 2 1 + 2 0 = 1024 + 512 + 256 + 128 + 64 + 16 + 2 + 1 = 2003 What is the range for unsigned values with w bits? 8

Signed Integers (1) Encoding positive numbers Same as unsigned numbers Encoding negative numbers Sign-magnitude representation Ones complement representation Two s complement representation 9

Signed Integers (2) Sign-magnitude representation b w-1 b w-2 b 1 b 0 Sign bit bw1 S( B) ( 1) Two zeros Magnitude w 2 i0 [000 00], [100 00] 2 1101 1100 1011 Used for floating-point numbers b i i 1111 1110-6 -7-5 -4-3 -2-1 1010 1001 0000 0001 0010 0 1 2 3 0 1000 7 4 5 6 0111 0110 0011 0100 0101 10

Signed Integers (3) Ones complement representation b w-1 b w-2 b 1 b 0 Sign bit w 2 w1 O( B) b w 1(2 1) bi 2 i0 Easy to find n Two zeros [000 00], [111 11] No longer used i 1101 1100 1011 1111 1110-1 0-2 -3-4 -5-6 1010 1001 0000 0001 0010 0 1 2 3-7 1000 7 4 5 6 0111 0110 0011 0100 0101 11

Signed Integers (4) Two s complement representation b w-1 b w-2 b 1 b 0 Sign bit w 2 w1 O( B) b w 1 2 bi 2 i0 Unique zero Easy for hardware leading 0 0 leading 1 < 0 1101 1100 1011 1111 1110-2 -1-3 -4-5 -6-7 1010 1001 Used by almost all modern machines i 0000 0001 0010 0 1 2 3-8 1000 7 4 5 6 0111 0110 0011 0100 0101 12

Signed Integers (5) Two s complement representation (cont d) Following holds for two s complement ~ x + 1 == x Complement Observation: ~x + x == 1111 11 2 == 1 Increment ~x + x == 1 ~x + x + ( x + 1) == 1 + ( x + 1) ~x + 1 == x 13

Numeric Ranges (1) Unsigned values UMin = 0 UMax = 2 w 1 [000 00] [111 11] Two s complement values TMin = 2 w-1 [100 00] TMax = 2 w-1 1 [011 11] Decimal Hex Binary Values for w = 16 UMax 65535 FF FF 11111111 11111111 TMax 32767 7F FF 01111111 11111111 TMin -32768 80 00 10000000 00000000-1 -1 FF FF 11111111 11111111 0 0 00 00 00000000 00000000 14

Numeric Ranges (2) Values for different word sizes w = 8 w = 16 w = 32 w = 64 UMax 255 65,535 4,294,967,295 18,446,744,073,709,551,615 TMax 127 32,767 2,147,483,647 9,223,372,036,854,775,807 TMin -128-32,768-2,147,483,648-9,223,372,036,854,775,808 Observations TMin = TMax + 1 (Asymmetric range) UMax = 2 * TMax + 1 In C programming #include <limits.h> INT_MIN, INT_MAX, LONG_MIN, LONG_MAX, UINT_MAX, Values platform-specific 15

Type Conversion (1) Unsigned: w bits w+k bits Zero extension: just fill k bits with 0 s unsigned short x = 2003; unsigned ix = (unsigned) x; x w = 16 0 0 0 0 0 1 1 1 1 1 0 1 0 0 1 1 ix 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 1 0 0 1 1 k = 16 w = 16 16

Type Conversion (2) Signed: w bits w+k bits Given w-bit signed integer x Convert it to w+k-bit integer with same value Sign extension Make k copies of sign bit X Sign bit w X k w 17

Type Conversion (3) Sign extension example Converting from smaller to larger integer type C automatically performs sign extension short int x = 2003; int ix = (int) x; short int y = -2003; int iy = (int) y; Decimal Hex Binary x 2003 07 D3 00000111 11010011 ix 2003 00 00 07 D3 00000000 00000000 00000111 11010011 y -2003 F8 2D 11111000 00101101 iy -2003 FF FF F8 2D 11111111 11111111 11111000 00101101 18

Type Conversion (4) Unsigned & Signed: w+k bits w bits Just truncate it to lower w bits Equivalent to computing x mod 2 w unsigned int x = 0xcafebabe; unsigned short ix = (unsigned short) x; int y = 0x2003beef; short iy = (short) y; Decimal Hex Binary x 3405691582 CA FE BA BE 11001010 11111110 10111010 10111110 ix 47806 BA BE 10111010 10111110 y 537116399 20 03 BE EF 00100000 00000011 10111110 11101111 iy -16657 BE EF 10111110 11101111 19

Type Conversion (5) Unsigned Signed The same bit pattern is interpreted as a signed number U 2 w 2 w 1 0 w1 2Tw ( x) w w1 Unsigned x, x 2 Two s complement +2 w 1 0, x x 2 w 1 2 2 unsigned short x = 2003; short ix = (short) x; unsigned short y = 0xbabe; short iy = (short) y; Decimal Hex Binary x 2003 07 D3 00000111 11010011 ix 2003 07 D3 00000111 11010011 y 47806 BA BE 10111010 10111110 iy -17730 BA BE 10111010 10111110 20

Type Conversion (6) Signed Unsigned Ordering inversion Negative Big positive T 2U w Two s complement +2 w 1 ( x) w x 2, x x, x 2 w 2 w 1 0 0 short x = 2003; unsigned short ix = (unsigned short) x; short y = -2003; unsigned short iy = (unsigned short) y; Decimal Hex Binary x 2003 07 D3 00000111 11010011 0 2 w 1 0 Unsigned ix 2003 07 D3 00000111 11010011 y -2003 F8 2D 11111000 00101101 iy 63533 F8 2D 11111000 00101101 21

Type Casting in C (1) Constants By default, considered to be signed integers Unsigned if have U or u as suffix 0U, 12345U, 0x1A2Bu Type casting Explicit casting int tx, ty; unsigned ux, uy; tx = (int) ux; uy = (unsigned) ty; Implicit casting via Assignments Procedure calls int f(unsigned); tx = ux; f(ty); 22

Type Casting in C (2) Expression evaluation If mix unsigned and signed in single expression, signed values implicitly cast to unsigned. Including comparison operations <, >, ==, <=, >= Expression Type Evaluation 0 == 0U -1 < 0-1 < 0U -1 > -2 (unsigned) -1 > -2 2147483647 > -2147483647-1 2147483647U > -2147483647-1 2147483647 > (int) 2147483648U unsigned True signed True unsigned? signed? unsigned? signed? unsigned? signed? 23

Type Casting in C (3) Example 1 Example 2 int main () { unsigned i; for (i = 10; i >= 0; i--) printf ( %u\n, i); } void copy_mem1 (char *src, char *dest, unsigned len) { unsigned i; for (i = 0; i < len; i++) *dest++ = *src++; } 24

Type Casting in C (4) Example 3 int sum_array (int a[], unsigned len) { int i; int result = 0; for (i = 0; i <= len - 1; i++) result += a[i]; } return result; 25

Type Casting in C (5) Example 4 #include <stdio.h> int main () { unsigned char c; } while ((c = getchar())!= EOF) putchar (c); 26

Type Casting in C (6) Lessons There are many tricky situations when you use unsigned integers hard to debug Do not use just because numbers are nonnegative Use only when you need collections of bits with no numeric interpretation ( flags ) Few languages other than C support unsigned integers 27