Bits, Bytes, and Integers

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

Bits, Bytes and Integers Part 1

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

Bits, Bytes, and Integers

BITS, BYTES, AND INTEGERS

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

Lecture 5-6: Bits, Bytes, and Integers

Systems Programming and Computer Architecture ( )

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

Arithmetic and Bitwise Operations on Binary Data

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

Computer Systems CEN591(502) Fall 2011

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

Representing and Manipulating Integers Part I

Bits, Bytes, and Integers August 26, 2009

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

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

Representing and Manipulating Integers. Jo, Heeseung

Bits, Bytes and Integers

Integers Sep 3, 2002

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

Integer Encoding and Manipulation

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

Arithmetic and Bitwise Operations on Binary Data

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

CSCI2467: Systems Programming Concepts

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

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

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

Foundations of Computer Systems

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

CS429: Computer Organization and Architecture

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

Bits and Bytes January 13, 2005

Bitwise Data Manipulation. Bitwise operations More on 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. Data Representa+on: Base- 2 number representa+on. Binary Representa+ons. Encoding Byte Values. Example Data Representa+ons

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

Integers II. CSE 351 Autumn Instructor: Justin Hsia

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

Computer Organization: A Programmer's Perspective

Integers II. CSE 351 Autumn 2018

Integers II. CSE 351 Autumn Instructor: Justin Hsia

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

Hardware: Logical View

Outline. Integer representa+on and opera+ons Bit opera+ons Floa+ng point numbers. Reading Assignment:

Outline. Integer representa+on and opera+ons Bit opera+ons Floa+ng point numbers. Reading Assignment:

Bits, Bytes, and Integers Part 2

Representing Integers

Bits, Bytes, and Integers. Bits, Bytes, and Integers. The Decimal System and Bases. Everything is bits. Converting from Decimal to Binary

Manipulating Integers

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

Sign & Magnitude vs 2 s Complement ( Signed Integer Representa:ons)

Lecture 2: Number Representa2on

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

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

CSC 8400: Computer Systems. Represen3ng and Manipula3ng Informa3on. Background: Number Systems

Data III & Integers I

Data III & Integers I

Integer Representation

REMEMBER TO REGISTER FOR THE EXAM.

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

Csci 2021 Class Web Pages. Data Representa?on: Bits, Bytes, and Integers. Binary Representa?ons. Bits, Bytes, and Integers

How a computer runs a program. Binary Representa8on and Opera8ons on Binary Data. Opera8ng System 9/17/13. You can view binary file contents

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

Floa.ng Point : Introduc;on to Computer Systems 4 th Lecture, Sep. 10, Instructors: Randal E. Bryant and David R.

Data Representa+on in Memory

Integer Representation Floating point Representation Other data types

Data III & Integers I

Floats are not Reals. BIL 220 Introduc6on to Systems Programming Spring Instructors: Aykut & Erkut Erdem

15110 Principles of Computing, Carnegie Mellon University - CORTINA. Digital Data

Computer Organization & Systems Exam I Example Questions

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

Arithmetic Operators. Portability: Printing Numbers

Simple Data Types in C. Alan L. Cox

Binary Arithme-c CS 64: Computer Organiza-on and Design Logic Lecture #2

UNIT IV: DATA PATH DESIGN

Roadmap. The Interface CSE351 Winter Frac3onal Binary Numbers. Today s Topics. Floa3ng- Point Numbers. What is ?

BBM 101 Introduc/on to Programming I Fall 2014, Lecture 3. Aykut Erdem, Erkut Erdem, Fuat Akal

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

Main Memory Organization

Integer Representation

CSE 351 Midterm - Winter 2017

CS 107 Lecture 3: Integer Representations

Tutorial 1: C-Language

Digital Systems. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

CS 270: Computer Organization. Integer Arithmetic Operations

CS107, Lecture 2 Bits and Bytes; Integer Representations

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

COMP2611: Computer Organization. Data Representation

Bits, Bytes, and Integers

Number systems & Bit opera1ons. h#p://xkcd.com/99/

CS 107 Lecture 2: Bits and Bytes (continued)

Data Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19

Instructor: Randy H. Katz hap://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #7. Warehouse Scale Computer

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Transcription:

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 University 1

Today: Bits, Bytes, and Integers Represen6ng informa6on as bits Bit- level manipula6ons Integers Representa=on: unsigned and signed Conversion, cas=ng Expanding, trunca=ng Addi=on, nega=on, mul=plica=on, shi@ing Summary 2

Binary Representa6ons Everything is a collec6on of bits (a bit: 0 or 1) By encoding/interpre6ng sets of bits in various ways Computers determine what to do (instruc6ons) and represent and manipulate numbers, sets, strings, etc Why bits? Electronic Implementa6on Easy to store with bistable elements Reliably transmijed on noisy and inaccurate wires 3.3V" 2.8V" 0.5V" 0.0V" 0" 1" 0" 3

For example, can count in binary Base 2 Number Representa6on Represent 15213 10 as 11101101101101 2 15213 10 3 10 0 1 10 1 2 10 2 5 10 3 1 10 4 011101101101101 2 = 1 2 0 + 0 2 1 + 1 2 2 + 1 2 3 + 0 2 4 + 1 2 5 + 1 2 6 + 0 2 7 + 1 2 8 + 1 2 9 + 0 2 10 + 1 2 11 + 1 2 12 + 1 2 13 + 0 2 14 4

Encoding Byte Values Byte = 8 bits Binary 000000002 to 111111112 Decimal: 010 to 25510 Hexadecimal 0016 to FF16 Base 16 number representa=on Use characters 0 to 9 and A to F Write FA1D37B16 in C as 0xFA1D37B 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

Example Data Representa6ons C Data Type Typical 32- bit Typical 64- bit x86-64 char 1 1 1 short 2 2 2 int 4 4 4 long 4 8 8 Typically treated as a signed value, but no guarantee! float 4 4 4 double 8 8 8 long double 10/16 pointer 4 8 8 Watch out for portability issues! (Use ISO C99 data types when necessary.) 6

Terminology: Binary representa=on for short x = 15213!!! 16 bits 011101101101101 2 = 1 2 0 + 0 2 1 + 1 2 2 + 1 2 3 + 0 2 4 + 1 2 5 + 1 2 6 + 0 2 7 + 1 2 8 + 1 2 9 + 0 2 10 + 1 2 11 + 1 2 12 + 1 2 13 + 0 2 14 + 0 2 15 most- significant bit (MSB) least- significant bit (LSB) 7

Conver6ng Between Different Bases Find the hexadecimal representa6on for the following numbers: 912559 12648430 2989 0xDECAF 0xC0FFEE 0xBAD How do you convert from decimal to hex? Take the value, mod it by 16 to find the quo=ent and remainder Take the reminder as the next digit (from least- significant to most) Repeat with the quo=ent as the new value it reaches 0 What about their binary representa6on? 8

Today: Bits, Bytes, and Integers Represen6ng informa6on as bits Bit- level manipula6ons Integers Representa=on: unsigned and signed Conversion, cas=ng Expanding, trunca=ng Addi=on, nega=on, mul=plica=on, shi@ing Summary Representa6ons in memory, pointers, strings 9

Boolean Algebra Developed by George Boole in 19th Century Algebraic representa=on of logic Encode True as 1 and False as 0 And n A&B = 1 when both A=1 and B=1 Or n A B = 1 when either A=1 or B=1 Not n ~A = 1 when A=0 Exclusive- Or (Xor) n A^B = 1 when either A=1 or B=1, but not both 10

General Boolean Algebras Operate on Bit Vectors Opera=ons applied bitwise 01101001 & 01010101 01000001 01101001 01010101 01111101 01101001 ^ 01010101 00111100 ~ 01010101 01000001 01111101 00111100 10101010 All of the Proper6es of Boolean Algebra Apply 11

Neat Tricks with XOR What does foo do? void foo(int *x, int *y) { *y = *x ^ *y; /* step 1 */ *x = *x ^ *y; /* step 2 */ *y = *x ^ *y; /* step 3 */ } XOR: Step *x *y ini=al a b 1 a a^b 2 a^(a^b) a^b 3 b (a^b)^b = a foo swaps the two numbers! 12

Example: Represen6ng & Manipula6ng Sets Representa6on Width w bit vector represents subsets of {0,, w 1} a j = 1 if j A 01101001 { 0, 3, 5, 6 } 76543210 01010101 { 0, 2, 4, 6 } 76543210 Opera6ons & Intersec=on 01000001 { 0, 6 } Union 01111101 { 0, 2, 3, 4, 5, 6 } ^ Symmetric difference 00111100 { 2, 3, 4, 5 } ~ Complement 10101010 { 1, 3, 5, 7 } 13

Bit- Level Opera6ons in C Opera6ons &,, ~, ^ 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 ~010000012 101111102 ~0x00 0xFF ~000000002 111111112 0x69 & 0x55 0x41 & 0x69 0x55 0x7D & 14

Bit- Level Opera6ons in C Opera6ons &,, ~, ^ 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 ~010000012 101111102 ~0x00 0xFF ~000000002 111111112 0x69 & 0x55 0x41 011010012 & 010101012 010000012 0x69 0x55 0x7D 011010012 010101012 011111012 15

Contrast: Logic Opera6ons in C Contrast to Logical Operators &&,,! View 0 as False Anything nonzero as True Always return 0 or 1 Early termina=on Examples (char data type)!0x41!0x00!!0x41 0x00 0x01 0x01 0x69 && 0x55 0x01 0x69 0x55 0x01 p && *p (avoids null pointer access) 16

Bit- Level Opera6ons in C Opera6ons &,, ~, ^ 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 ~010000012 101111102 ~0x00 0xFF ~000000002 111111112 0x69 & 0x55 0x41 011010012 & 010101012 010000012 0x69 0x55 0x7D 011010012 010101012 011111012 17

Contrast: Logic Opera6ons in C Contrast to Logical Operators &&,,! View 0 as False Anything nonzero as True Always return 0 or 1 Early termina=on Examples (char data type)!0x41 0x00!0x00 0x01!!0x41 0x01 Watch out for && vs. & (and vs. ) one of the more common oopsies in C programming 0x69 && 0x55 0x01 0x69 0x55 0x01 p && *p (avoids null pointer access) 18

Shic Opera6ons Lec Shic: x << y Shi@ bit- vector x le@ y posi=ons Throw away extra bits on le@ Fill with 0 s on right Right Shic: x >> y Shi@ bit- vector x right y posi=ons Throw away extra bits on right Logical shi@ Fill with 0 s on le@ Arithme=c shi@ Replicate most significant bit on le@ Undefined Behavior Shi@ amount < 0 or word size Argument x 01100010 << 3 Log. >> 2 Arith. >> 2 00010000 00011000 00011000 Argument x 10100010 << 3 Log. >> 2 Arith. >> 2 00010000 00101000 11101000 For unsigned data >> performs logical shi@. For signed data, the standard does not define it, but likely arithme=c. 19

Another Puzzle for Your Amusement What does bar do? void bar(int x, int y) { return (x & ~y) (~x & y); } (x & ~y): if a bit in y is set, clear that same bit in x (~x & y): if a bit in x is set, clear that same bit in y (x&~y) (~x&y): ret 0 y 1 x 0 1 0 1 1 0 bar computes XOR! 20

Today: Bits, Bytes, and Integers Represen6ng informa6on as bits Bit- level manipula6ons Integers Representa6on: unsigned and signed Conversion, cas=ng Expanding, trunca=ng Addi=on, nega=on, mul=plica=on, shi@ing Summary Representa6ons in memory, pointers, strings Summary 21

Encoding Integers Unsigned w 1 B2U(X) = x i 2 i i=0 Two s Complement w 2 B2T(X) = x w 1 2 w 1 + x i 2 i i=0 short int x = 15213; short int y = -15213; Sign Bit C short 2 bytes long Decimal Hex Binary x 15213 3B 6D 00111011 01101101 y -15213 C4 93 11000100 10010011 Sign Bit For 2 s complement, most significant bit indicates sign 0 for nonnega=ve 1 for nega=ve 22

Two- complement Encoding Example (Cont.) x = 15213: 00111011 01101101 y = -15213: 11000100 10010011 Weight 15213-15213 1 1 1 1 1 2 0 0 1 2 4 1 4 0 0 8 1 8 0 0 16 0 0 1 16 32 1 32 0 0 64 1 64 0 0 128 0 0 1 128 256 1 256 0 0 512 1 512 0 0 1024 0 0 1 1024 2048 1 2048 0 0 4096 1 4096 0 0 8192 1 8192 0 0 16384 0 0 1 16384-32768 0 0 1-32768 Sum 15213-15213 23

Numeric Ranges How many of the following statements are FALSE? The maximum value an unsigned short (16 bits) can represent is 0xFFFF. The minimum value of a signed short can represent is 0xFFFF. 0x8000 The maximum value of a signed short can represent is 0x7FFF. 24

Numeric Ranges Unsigned Values UMin = 0 0x000 0 UMax = 2 w 1 0xFFF 1 Two s Complement Values TMin = 2 w 1 0x800 0 TMax = 2 w 1 1 0x7FF F Other Values Minus 1 0xFF F Values for W = 16 Decimal Hex Binary 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 25

Values for Different Word Sizes W 8 16 32 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 Observa6ons TMin = TMax + 1 Asymmetric range UMax = 2 * TMax + 1 C Programming #include <limits.h> Declares constants, e.g., ULONG_MAX LONG_MAX LONG_MIN Values pla~orm specific 26

Unsigned & Signed Numeric Values X B2U(X) B2T(X) 0000 0 0 0001 1 1 0010 2 2 0011 3 3 0100 4 4 0101 5 5 0110 6 6 0111 7 7 1000 8 8 1001 9 7 1010 10 6 1011 11 5 1100 12 4 1101 13 3 1110 14 2 1111 15 1 Equivalence Same encodings for nonnega=ve values Uniqueness Every bit paern represents unique integer value Each representable integer has unique bit encoding 27

Today: Bits, Bytes, and Integers Represen6ng informa6on as bits Bit- level manipula6ons Integers Representa=on: unsigned and signed Conversion, cas6ng Expanding, trunca=ng Addi=on, nega=on, mul=plica=on, shi@ing Summary Representa6ons in memory, pointers, strings 28

Mapping Between Signed & Unsigned Two s Complement x T2B T2U X B2U Unsigned ux Maintain Same Bit Paern Unsigned ux U2B U2T X B2T Two s Complement x Maintain Same Bit Paern Mappings between unsigned and two s complement numbers: Keep bit representa6ons and reinterpret 29

30 Mapping Signed Unsigned Signed 0 1 2 3 4 5 6 7-8 -7-6 -5-4 -3-2 -1 Unsigned 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Bits 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 U2T T2U

31 Mapping Signed Unsigned Signed 0 1 2 3 4 5 6 7-8 -7-6 -5-4 -3-2 -1 Unsigned 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Bits 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 = +/- 16

Rela6on between Signed & Unsigned Two s Complement x T2B T2U X B2U Unsigned ux Maintain Same Bit Paern ux x w 1 0 + + + + + + - + + + + + Large nega6ve weight becomes Large posi6ve weight 32

Conversion Visualized 2 s Comp. Unsigned Ordering Inversion Nega=ve Big Posi=ve UMax UMax 1 TMax TMax + 1 TMax Unsigned Range 2 s Complement Range 0 1 2 TMin 0 Watch out for conversion - - - values may change! 33

Signed vs. Unsigned in C Constants By default are considered to be signed integers Unsigned if have U as suffix 0U, 4294967259U Cas6ng Explicit cas=ng between signed & unsigned same as U2T and T2U int tx, ty; unsigned ux, uy; tx = (int) ux; uy = (unsigned) ty; Implicit cas=ng also occurs via assignments and procedure calls tx = ux; uy = ty; 34

Cas6ng Surprises Expression Evalua6on If there is a mix of unsigned and signed in single expression, signed values implicitly cast to unsigned Including comparison opera=ons <, >, ==, <=, >= Examples for W = 32: TMIN = - 2,147,483,648, TMAX = 2,147,483,647 35

Expression Evalua6on Puzzles Assuming int type (32 bits) Constant 1 Constant 2 Rela6on Evalua6on 0 0 0U == unsigned - 1-1 0 < signed - 1-1 0U > unsigned 2147483647-2147483647- 1-2147483648 > signed 2147483647U - 2147483647-1 -2147483648 < unsigned - 1-1 - 2-2 > signed (unsigned)- 1-1 - 2-2 > unsigned 2147483647 2147483648U < unsigned 2147483647 (int) 2147483648U > signed TMIN = - 2147483648 (0x8000000) TMAX = 2147483647 (0x7FFFFFFF) 36

Summary Cas6ng Signed Unsigned: Basic Rules Bit pajern is maintained But reinterpreted Can have unexpected effects: adding or subtrac6ng 2 w Expression containing signed and unsigned int int is cast to unsigned!! 37