Bits, Bytes, and Integers Part 2

Similar documents
Bits, Bytes, and Integers

Bits, Bytes, and Integers August 26, 2009

Lecture 5-6: Bits, Bytes, and Integers

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

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

Foundations of Computer Systems

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

Integers Sep 3, 2002

CS 270: Computer Organization. Integer Arithmetic Operations

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

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

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

Computer Systems CEN591(502) Fall 2011

Systems Programming and Computer Architecture ( )

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

Arithmetic and Bitwise Operations on Binary Data

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

Bits and Bytes January 13, 2005

Manipulating Integers

Representing and Manipulating Integers. Jo, Heeseung

Arithmetic and Bitwise Operations on Binary Data

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

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

Bits, Bytes and Integers

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

CS429: Computer Organization and Architecture

Computer Organization: A Programmer's Perspective

Byte Ordering. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Bits, Bytes and Integers Part 1

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

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

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: Data Presentation Mohamed Zahran (aka Z)

Integer Encoding and Manipulation

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

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

Hardware: Logical View

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

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

Bits, Bytes, and Integers

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

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

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

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

BITS, BYTES, AND INTEGERS

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

Bits, Bytes, and Integers. Data Representa+on: Base- 2 number representa+on. Binary Representa+ons. Encoding Byte Values. Example Data Representa+ons

Bits, Bytes, and Integers

Bits, Bytes, and Integer

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

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Integers II. CSE 351 Autumn 2018

CSE351: Memory, Data, & Addressing I

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Integer Representation Floating point Representation Other data types

Memory, Data, & Addressing II CSE 351 Spring

Representing and Manipulating Integers Part I

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

Representing Integers

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

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

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

System Programming CISC 360. Floating Point September 16, 2008

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

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

REMEMBER TO REGISTER FOR THE EXAM.

CHW 261: Logic Design

Integer Representation

Module 2: Computer Arithmetic

COMP2121: Microprocessors and Interfacing. Number Systems

CS 265. Computer Architecture. Wei Lu, Ph.D., P.Eng.

CS 33. Data Representation (Part 3) CS33 Intro to Computer Systems VIII 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Inf2C - Computer Systems Lecture 2 Data Representation

CS367 Test 1 Review Guide

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

Binary Representations and Arithmetic

Memory, Data, & Addressing II

COMP2611: Computer Organization. Data Representation

Computer Organization & Systems Exam I Example Questions

Outline. x86 Architecture

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: Bits and Bytes and Numbers

COMP Overview of Tutorial #2

Data III & Integers I

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Course overview. Computer Organization and Assembly Languages Yung-Yu Chuang 2006/09/18. with slides by Kip Irvine

Topic Notes: Bits and Bytes and Numbers

AGENDA Binary Operations CS 3330 Samira Khan

Simple Data Types in C. Alan L. Cox

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

Computer (Literacy) Skills. Number representations and memory. Lubomír Bulej KDSS MFF UK

CSCI2467: Systems Programming Concepts

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

Introduction to C. Why C? Difference between Python and C C compiler stages Basic syntax in C

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

CSE 351 Midterm - Winter 2017

Data III & Integers I

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

CSE 351 Midterm - Winter 2015 Solutions

COMP Primitive and Class Types. Yi Hong May 14, 2015

Transcription:

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: Thursday, Feb 1, 11:59:00 pm Last Possible Time to Turn in: Sun, Feb 4, 11:59PM Read the instructions carefully Please start ASAP Seek help (office hours start today!) Based on Lecture 2, 3, and 4 After today s lecture you know everything for the integer problems, float problems covered on Thursday 2

Summary From Last Lecture Representing information as bits Bit-level manipulations Integers Representation: unsigned and signed Conversion, casting Expanding, truncating Addition, negation, multiplication, shifting Representations in memory, pointers, strings Summary 3

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 ~010000012 00012 101111102 11102 ~0x00 0xFF ~000000002 00002 111111112 11112 0x69 & 0x55 0x41 011010012 10012 & 010101012 01012 010000012 0100 00012 0x69 0x55 0x7D 011010012 10012 010101012 01012 011111012 0111 11012 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 4

Logic Operations in C Logic Operations: &&,,! 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) 5

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 Sign Bit Two s Complement Examples (w = 5) 10 = -16 8 4 2 1 0 1 0 1 0 8+2 = 10-10 = -16 8 4 2 1 1 0 1 1 0-16+4+2 = -10 6

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 nonnegative values Uniqueness Every bit pattern represents unique integer value Each representable integer has unique bit encoding Expression containing signed and unsigned int: int is cast to unsigned!! 7

Sign Extension and Truncation Sign Extension Truncation 8

Today: Bits, Bytes, and Integers Representing information as bits Bit-level manipulations Integers Representation: unsigned and signed Conversion, casting Expanding, truncating Addition, negation, multiplication, shifting Representations in memory, pointers, strings Summary 9

Unsigned Addition Operands: w bits True Sum: w+1 bits Standard Addition Function Ignores carry output Implements Modular Arithmetic s = UAdd w (u, v) = u + v mod 2 w + u v u + v Discard Carry: w bits UAdd w (u, v) unsigned char 1110 1001 + 1101 0101 E9 + D5 223 + 213 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 10

Unsigned Addition Operands: w bits True Sum: w+1 bits Standard Addition Function Ignores carry output Implements Modular Arithmetic s = UAdd w (u, v) = u + v mod 2 w + u v u + v Discard Carry: w bits UAdd w (u, v) unsigned char 1110 1001 + 1101 0101 1 1011 1110 1011 1110 E9 + D5 1BE BE 223 + 213 446 190 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 11

Visualizing (Mathematical) Integer Addition Integer Addition 4-bit integers u, v Compute true sum Add 4 (u, v) Values increase linearly with u and v Forms planar surface 32 28 24 20 Add 4 (u, v) Integer Addition 16 12 8 4 0 0 2 u 4 6 8 10 12 14 0 2 4 6 8 v 10 12 14 12

Visualizing Unsigned Addition Wraps Around If true sum 2 w At most once Overflow UAdd 4 (u, v) True Sum 2 w+1 Overflow 16 14 12 10 2 w 0 Modular Sum 8 6 4 2 0 0 u 2 4 6 8 10 12 14 0 2 4 6 8 10 12 v 14 13

Two s Complement Addition Operands: w bits True Sum: w+1 bits u + v u + v Discard Carry: w bits TAdd w (u, v) TAdd and UAdd 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 1110 1001 + 1101 0101 1 1011 1110 1011 1110 E9 + D5 1BE BE -23 + -43 446-66 14

TAdd Overflow Functionality True sum requires w+1 bits Drop off MSB Treat remaining bits as 2 s comp. integer 0 111 1 0 100 0 0 000 0 True Sum 2 w 1 2 w 1 1 0 PosOver TAdd Result 011 1 000 0 1 011 1 2 w 1 100 0 1 000 0 2 w NegOver 15

Visualizing 2 s Complement Addition NegOver Values 4-bit two s comp. Range from -8 to +7 Wraps Around TAdd 4 (u, v) If sum 2 w 1 Becomes negative At most once If sum < 2 w 1 Becomes positive At most once 8 6 4 2 0-2 -4-6 -8-8 -6-4 -2 u 0 2 4 6-8 -6-4 -2 6 4 2 0 v PosOver 16

Characterizing TAdd Functionality True sum requires w+1 bits Drop off MSB Treat remaining bits as 2 s comp. integer TAdd(u, v) > 0 v < 0 Negative Overflow < 0 > 0 u Positive Overflow TAd w (u,v) = u + v + w 1 u + v < TMin 2 w (NegOver) w u + v TMin w u + v TMax w u + v 2 w 1 w TMax w < u + v (PosOver) 17

Negation: Complement & Increment Negate through complement and increase ~x + 1 == -x Example Observation: ~x + x == 1111 111 == -1 + x ~x -1 1 0 0 1 1 1 0 1 0 1 1 0 0 0 1 0 1 1 1 1 1 1 1 1 x = 15213 Decimal Hex Binary x 15213 3B 6D 00111011 01101101 ~x -15214 C4 92 11000100 10010010 ~x+1-15213 C4 93 11000100 10010011 y -15213 C4 93 11000100 10010011 18

Complement & Increment Examples x = 0 Decimal Hex Binary 0 0 00 00 00000000 00000000 ~0-1 FF FF 11111111 11111111 ~0+1 0 00 00 00000000 00000000 x = TMin Decimal Hex Binary x -32768 80 00 10000000 00000000 ~x 32767 7F FF 01111111 11111111 ~x+1-32768 80 00 10000000 00000000 Canonical counter example 19

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+1 + 1 Two s complement min (negative): Up to 2w-1 bits Result range: x * y ( 2 w 1 )*(2 w 1 1) = 2 2w 2 + 2 w 1 Two s complement max (positive): Up to 2w bits, but only for (TMin 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 20

Unsigned Multiplication in C Operands: w bits True Product: 2*w bits Discard w bits: w bits u v u * v UMult w (u, v) Standard Multiplication Function Ignores high order w bits Implements Modular Arithmetic UMult w (u, v) = u v mod 2 w 1110 1001 * 1101 0101 1100 0001 1101 0010 1101 1101 E9 * D5 C1DD DD 223 * 213 47499 221 21

Signed Multiplication in C Operands: w bits True Product: 2*w bits Discard w bits: w bits u v u * v TMult w (u, v) Standard Multiplication Function Ignores high order w bits Some of which are different for signed vs. unsigned multiplication Lower bits are the same 1110 1001 * 1101 0101 1100 0001 1101 0010 1101 1101 E9 * D5 C1DD DD -23 * -43 16896-35 22

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 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 Discard k bits: w bits UMult w (u, 2 k ) TMult w (u, 2 k ) 0 0 1 0 0 0 0 0 0 k 0 0 0 23

Break Time! taradiddle: "pretentious nonsense" Check out: quiz: day 3: operators https://canvas.cmu.edu/courses/3822/quizzes/9020 24

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: u / 2 k 0 0 0 Result: u / 2 k 0 0 1 0 0 0 0 0 0. Binary Point Division Computed 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 25

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 0 0 1 0 0 0 0. 0 Binary Point Division Computed 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 26

Correct Power-of-2 Divide 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 Case 1: No rounding Dividend: Divisor: u +2 k 1 / 2 k u / 2 k k 1 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 0 0 0 01 1 1 1. Binary Point 1 1 1 Biasing has no effect 27

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

Today: Bits, Bytes, and Integers Representing information as bits Bit-level manipulations Integers Representation: unsigned and signed Conversion, casting Expanding, truncating Addition, negation, multiplication, shifting Summary Representations in memory, pointers, strings 29

Arithmetic: Basic Rules Addition: Unsigned/signed: Normal addition followed by truncate, same operation on bit level Unsigned: addition mod 2 w Mathematical addition + possible subtraction of 2 w Signed: modified addition mod 2 w (result in proper range) Mathematical addition + possible addition or subtraction of 2 w Multiplication: Unsigned/signed: Normal multiplication followed by truncate, same operation on bit level Unsigned: multiplication mod 2 w Signed: modified multiplication mod 2 w (result in proper range) 30

Why Should I Use Unsigned? Don t use without understanding implications Easy to make mistakes unsigned i; for (i = cnt-2; i >= 0; i--) a[i] += a[i+1]; Can be very subtle #define DELTA sizeof(int) int i; for (i = CNT; i-delta >= 0; i-= DELTA)... 31

Counting Down with Unsigned Proper way to use unsigned as loop index unsigned i; for (i = cnt-2; i < cnt; i--) a[i] += a[i+1]; See Robert Seacord, Secure Coding in C and C++ C Standard guarantees that unsigned addition will behave like modular arithmetic 0 1 UMax Even better size_t i; for (i = cnt-2; i < cnt; i--) a[i] += a[i+1]; Data type size_t defined as unsigned value with length = word size Code will work even if cnt = UMax What if cnt is signed and < 0? 32

Why Should I Use Unsigned? (cont.) Do Use When Performing Modular Arithmetic Multiprecision arithmetic Do Use When Using Bits to Represent Sets Logical right shift, no sign extension Do Use In System Programming Bit masks, device commands, 33

Integer Arithmetic Example unsigned char 1111 0011 + 0101 0010 1 0100 0101 0101 0101 unsigned char 0001 1001 * 0000 0010 0 0011 0010 F3 + 52 145 45 19 * 02 032 243 + 82 325 69 25 * 2 50 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 0011 0010 32 50 34

Today: Bits, Bytes, and Integers Representing information as bits Bit-level manipulations Integers Representation: unsigned and signed Conversion, casting Expanding, truncating Addition, negation, multiplication, shifting Summary Representations in memory, pointers, strings 35

Byte-Oriented Memory Organization Programs refer to data by address Conceptually, envision it as a very large array of bytes In reality, it s not, but can think of it that way An address is like an index into that array and, a pointer variable stores an address Note: system provides private address spaces to each process Think of a process as a program being executed So, a program can clobber its own data, but not that of others 36

Machine Words Any given computer has a Word Size Nominal size of integer-valued data and of addresses Until recently, most machines used 32 bits (4 bytes) as word size Limits addresses to 4GB (2 32 bytes) Increasingly, machines have 64-bit word size Potentially, could have 18 EB (exabytes) of addressable memory That s 18.4 X 10 18 Machines still support multiple data formats Fractions or multiples of word size Always integral number of bytes 37

Word-Oriented Memory Organization Addresses Specify Byte Locations Address of first byte in word Addresses of successive words differ by 4 (32-bit) or 8 (64-bit) 32-bit Words Addr = 0000?? Addr = 0004?? Addr = 0008?? Addr = 0012?? 64-bit Words Addr = 0000?? Addr = 0008?? Bytes Addr. 0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 0015 38

Example Data Representations 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 float 4 4 4 double 8 8 8 pointer 4 8 8 39

Byte Ordering So, how are the bytes within a multi-byte word ordered in memory? Conventions Big Endian: Sun, PPC Mac, Internet Least significant byte has highest address Little Endian: x86, ARM processors running Android, ios, and Windows Least significant byte has lowest address 40

Byte Ordering Example Example Variable x has 4-byte value of 0x01234567 Address given by &x is 0x100 Big Endian Little Endian 0x100 0x101 0x102 0x103 01 23 45 67 0x100 0x101 0x102 0x103 67 45 23 01 41

Representing Integers Decimal: 15213 Binary: 0011 1011 0110 1101 Hex: 3 B 6 D Increasing addresses int A = 15213; IA32, x86-64 6D 3B 00 00 IA32, x86-64 93 C4 FF FF Sun 00 00 3B 6D int B = -15213; Sun FF FF C4 93 long int C = 15213; IA32 6D 3B 00 00 x86-64 6D 3B 00 00 00 00 00 00 Two s complement representation Sun 00 00 3B 6D 42

Examining Data Representations Code to Print Byte Representation of Data Casting pointer to unsigned char * allows treatment as a byte array typedef unsigned char *pointer; void show_bytes(pointer start, size_t len){ size_t i; for (i = 0; i < len; i++) printf( %p\t0x%.2x\n",start+i, start[i]); printf("\n"); } Printf directives: %p: Print pointer %x: Print Hexadecimal 43

show_bytes Execution Example int a = 15213; printf("int a = 15213;\n"); show_bytes((pointer) &a, sizeof(int)); Result (Linux x86-64): int a = 15213; 0x7fffb7f71dbc 6d 0x7fffb7f71dbd 3b 0x7fffb7f71dbe 00 0x7fffb7f71dbf 00 44

Representing Pointers int B = -15213; int *P = &B; Sun EF FF FB 2C IA32 AC 28 F5 FF x86-64 3C 1B FE 82 FD 7F 00 00 Different compilers & machines assign different locations to objects Even get different results each time run program 45

Representing Strings Strings in C Represented by array of characters Each character encoded in ASCII format Standard 7-bit encoding of character set Character 0 has code 0x30 Digit i has code 0x30+i String should be null-terminated Final character = 0 Compatibility Byte ordering not an issue char S[6] = "18213"; IA32 31 38 32 31 33 00 Sun 31 38 32 31 33 00 46

Reading Byte-Reversed Listings Disassembly Text representation of binary machine code Generated by program that reads the machine code Example Fragment Address Instruction Code Assembly Rendition 8048365: 5b pop %ebx 8048366: 81 c3 ab 12 00 00 add $0x12ab,%ebx 804836c: 83 bb 28 00 00 00 00 cmpl $0x0,0x28(%ebx) Deciphering Numbers Value: 0x12ab Pad to 32 bits: 0x000012ab Split into bytes: 00 00 12 ab Reverse: ab 12 00 00 47

Integer C Puzzles Initialization int x = foo(); int y = bar(); unsigned ux = x; unsigned uy = y; 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 >= 0 (x -x)>>31 == -1 ux >> 3 == ux/8 x >> 3 == x/8 x & (x-1)!= 0 48

Integer C Puzzles Initialization int x = foo(); int y = bar(); unsigned ux = x; unsigned uy = y; 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 >= 0 (x -x)>>31 == -1 ux >> 3 == ux/8 x >> 3 == x/8 x & (x-1)!= 0 49

Summary Representing information as bits Bit-level manipulations Integers Representation: unsigned and signed Conversion, casting Expanding, truncating Addition, negation, multiplication, shifting Representations in memory, pointers, strings Summary 50