ICS 2005 Instructor: Peter A. Dinda TA: Bin Lin Recitation 2.

Slides:



Advertisements
Similar presentations
– 1 – CS213, S’06 Floating Point 4/5/2006 Topics IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties CS213.
Advertisements

Fabián E. Bustamante, Spring 2007 Integers Today Numeric Encodings Programming Implications Basic operations Programming Implications Next time Floats.
University of Washington CSE351 Announcements:  HW0, having fun?  Use discussion boards!  Sign up for mailing list  If you enrolled recently,
CSCE 212 Computer Organization Lecture 2 Data Representation.
CS213 Topics Numeric Encodings Unsigned & Two’s complement Programming Implications C promotion rules Basic operations Addition, negation, multiplication.
Bits and Bytes 4/2/2008 Topics Physics, transistors, Moore’s law Why bits? Representing information as bits Binary/Hexadecimal Byte representations »numbers.
Bits and Bytes CS 213 Aug. 27, 1998 Topics Why bits? Representing information as bits –Binary/Hexadecimal –Byte representations »numbers »characters and.
Bits and Bytes January 17, 2002 Topics Why bits? Representing information as bits –Binary/Hexadecimal –Byte representations »numbers »characters and strings.
Bits, Bytes, and Integers August 29, 2007 Topics Representing information as bits Bit-level manipulations Boolean algebra Expressing in C Representations.
“The course that gives CMU its Zip!” Topics Numeric Encodings –Unsigned & Two’s complement Programming Implications –C promotion rules Basic operations.
CS213 Integers Apr 3, 2006 Topics Numeric Encodings
Bits and Bytes Topics Physics, transistors, Moore’s law Why bits? Representing information as bits Binary/Hexadecimal Byte representations »numbers »characters.
“The course that gives CMU its Zip!” Topics Numeric Encodings Unsigned & Two’s complement Programming Implications C promotion rules Basic operations.
Bits and Bytes January 15, 2004 Topics Why bits? Representing information as bits Binary/Hexadecimal Byte representations »numbers »characters and strings.
Bits and Bytes Topics Why bits? Representing information as bits Binary/Hexadecimal Byte representations »numbers »characters and strings »Instructions.
February 4, 2003 CSCE 212 Computer Architecture Lecture 3 Representing Integers.
Fabián E. Bustamante, Spring 2007 Bits and Bytes Today Why bits? Binary/hexadecimal Byte representations Boolean algebra Expressing in C.
Topics Numeric Encodings Unsigned & Two’s complement Programming Implications C promotion rules Basic operations Addition, negation, multiplication Programming.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bits, Bytes, and Integers MCS-284 : Computer Organization.
Carnegie Mellon 1 Bits, Bytes, and Integers Lecture, Jan. 24, 2013 These slides are from website which accompanies the book “Computer.
1 Integer Representations. 2 Outline Encodings –Unsigned and two’s complement Conversions –Signed vs. unsigned –Long vs. short Suggested reading –Chap.
Floating Point Numbers Topics –IEEE Floating Point Standard –Rounding –Floating Point Operations –Mathematical properties.
CS 270: Computer Organization Bits, Bytes, and Integers
Bits and Bytes Spring, 2015 Topics Why bits? Representing information as bits Binary / Hexadecimal Byte representations »Numbers »Characters and strings.
Bits and Bytes Topics Representing information as bits Bit-level manipulations Boolean algebra Expressing in C.
Lecture 3: Computation and Representation CS 2011 Fall 2014, Dr. Rozier.
Carnegie Mellon 1 Bits, Bytes, and Integers : Introduction to Computer Systems 3 rd Lectures, May 27th, 2014 Instructors: Greg Kesden.
Topics Numeric Encodings Unsigned & Two’s complement Programming Implications C promotion rules Basic operations Addition, negation, multiplication Programming.
Bits and Bytes Topics Why bits? Tell me Representing information as bits Binary/Hexadecimal Byte representations »numbers »characters and strings »Instructions.
Bits, Bytes, and Integers Topics Representing information as bits Bit-level manipulations Boolean algebra Expressing in C Representations of Integers Basic.
1 Saint Louis University Arithmetic and Bitwise Operations on Binary Data CSCI 224 / ECE 317: Computer Architecture Instructor: Prof. Jason Fritts Slides.
Bits and Bytes September 2, 2004 Topics Why bits? Representing information as bits Binary / Hexadecimal Byte representations »Numbers »Characters and strings.
– 1 – Number Systems Number of symbols = base of the system Most intuitive -- base 10 (decimal system) counting on fingertips symbols
CS 105 “Tour of the Black Holes of Computing” Topics Numeric Encodings Unsigned & Two’s complement Programming Implications C promotion rules Basic operations.
Info stored in computer (memory) Numbers All in binaray – can be converted to octal, hex Characters ASCII – 1-byte/char Unicode – 2-byte/char Unicode-table.com/en.
Carnegie Mellon 1 This week: Bits, Bytes, and Integers Representing information as bits Bit-level manipulations Integers  Representation: unsigned and.
Bits and Bytes Boolean Algebra ( ) Boolean algebra Expressing in C.
CS 105 “Tour of the Black Holes of Computing” Topics Numeric Encodings Unsigned & Two’s complement Programming Implications C promotion rules Basic operations.
Bits and Bytes Topics Why bits? Representing information as bits
Integers Topics Representations of Integers Basic properties and operations Implications for C.
Carnegie Mellon Bits, Bytes, and Integers (1-2) /18-243: Introduction to Computer Systems 2 nd Lecture, 19 May 2011 Instructors: Gregory Kesden.
Bits, Bytes, and Integers September 1, 2006 Topics Representing information as bits Bit-level manipulations Boolean algebra Expressing in C Representations.
Lecture 2 Two’s Complement Proofs Topics Two’s complement January 11, 2016 CSCE 212H Computer Architecture.
1 Manipulating Information (1). 2 Outline Bit-level operations Suggested reading –2.1.7~
Lecture 2 Ints revisited Topics Unsigned Integers Signed magnitude Two’s complement January 11, 2016 CSCE 212H Computer Architecture.
Number Systems and Representations Binary Representation Binary Representation Signed numbers Signed numbers Very small and very big numbers Very small.
Today’s Instructor: Randy Bryant
Recitation 4&5 and review 1 & 2 & 3
Integers Topics Numeric Encodings (2.2) Programming Implications
Bits, Bytes, and Integers CSE 238/2038/2138: Systems Programming
Instructor: David Ferry
CS213 Integers Topics Numeric Encodings Programming Implications
CS 105 “Tour of the Black Holes of Computing”
CS 105 “Tour of the Black Holes of Computing”
Bits and Bytes Topics Representing information as bits
Bits and Bytes Topics Representing information as bits
Bits and Bytes Boolean algebra Expressing in C
Bits and Bytes Topics Representing information as bits
Bits, Bytes, and Integers 2nd Lectures
Bits and Bytes Topics Representing information as bits
“The Class That Gives CMU Its Zip!”
1 The Hardware/Software Interface CSE351 Spring 2011 Module 3: Integers Monday, April 4, 2011.
Bits and Bytes Topics Representing information as bits
Comp Org & Assembly Lang
Comp Org & Assembly Lang
Computer Organization COMP 210
Comp Org & Assembly Lang
“The Class That Gives CMU Its Zip!”
“The Class That Gives CMU Its Zip!”
Lecture 2: Bits, Bytes, Ints
Presentation transcript:

ICS 2005 Instructor: Peter A. Dinda TA: Bin Lin Recitation 2

– 2 – , F’02 Data Representations Sizes of C Objects (in Bytes) C Data TypeCompaq AlphaTypical 32-bitIntel IA32 int444 long int844 char111 short222 float444 double888 long double8810/12 char *844 »Or any other pointer

– 3 – , F’02 Byte Ordering How should bytes within multi-byte word be ordered in memory? Conventions Sun’s, Mac’s are “Big Endian” machines Least significant byte has highest address Alphas, PC’s are “Little Endian” machines Least significant byte has lowest address

– 4 – , F’02 Byte Ordering Example Big Endian Least significant byte has highest address Little Endian Least significant byte has lowest addressExample Variable x has 4-byte representation 0x Address given by &x is 0x100 0x1000x1010x1020x x1000x1010x1020x Big Endian Little Endian

– 5 – , F’02 Reading Byte-Reversed Listings Disassembly Text representation of binary machine code Generated by program that reads the machine code Example Fragment AddressInstruction CodeAssembly Rendition :5b pop %ebx :81 c3 ab add $0x12ab,%ebx c:83 bb cmpl $0x0,0x28(%ebx) Deciphering Numbers Value: 0x12ab Pad to 4 bytes: 0x000012ab Split into bytes: ab Reverse: ab

– 6 – , F’02 Examining Data Representations Code to Print Byte Representation of Data Casting pointer to unsigned char * creates byte array typedef unsigned char *pointer; void show_bytes(pointer start, int len) { int i; for (i = 0; i < len; i++) printf("0x%p\t0x%.2x\n", start+i, start[i]); printf("\n"); } Printf directives: %p :Print pointer %x :Print Hexadecimal

– 7 – , F’02 show_bytes Execution Example int a = 15213; printf("int a = 15213;\n"); show_bytes((pointer) &a, sizeof(int)); Result (Linux): int a = 15213; 0x11ffffcb80x6d 0x11ffffcb90x3b 0x11ffffcba0x00 0x11ffffcbb0x00

– 8 – , F’02 Representing Floats Float F = ; IEEE Single Precision Floating Point Representation Hex: D B Binary: : Not same as integer representation, but consistent across machines 00 B4 6D 46 Linux/Alpha F B D Sun F Can see some relation to integer representation, but not obvious IEEE Single Precision Floating Point Representation Hex: D B Binary: : IEEE Single Precision Floating Point Representation Hex: D B Binary: :

– 9 – , F’02 Boolean Algebra Developed by George Boole in 19th Century Algebraic representation of logic Encode “True” as 1 and “False” as 0And A&B = 1 when both A=1 and B=1 Not ~A = 1 when A=0 Or A|B = 1 when either A=1 or B=1 Exclusive-Or (Xor) A^B = 1 when either A=1 or B=1, but not both

– 10 – , F’02 Cool Stuff with Xor void funny(int *x, int *y) { *x = *x ^ *y; /* #1 */ *x = *x ^ *y; /* #1 */ *y = *x ^ *y; /* #2 */ *y = *x ^ *y; /* #2 */ *x = *x ^ *y; /* #3 */ *x = *x ^ *y; /* #3 */} Bitwise Xor is form of addition With extra property that every value is its own additive inverse A ^ A = 0 BA Begin BA^B 1 (A^B)^B = AA^B 2 A(A^B)^A = B 3 AB End *y*x

– 11 – , F’02 Bit-Level Operations in C Operations &, |, ~, ^ Available in C Apply to any “integral” data type long, int, short, char View arguments as bit vectors Arguments applied bit-wise Examples (Char data type) ~0x41 --> 0xBE ~ > ~0x00 --> 0xFF ~ > x69 & 0x55 --> 0x & > x69 | 0x55 --> 0x7D | >

– 12 – , F’02 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)

– 13 – , F’02 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 most significant bit on right Useful with two’s complement integer representation Argument x << Log. >> Arith. >> Argument x << Log. >> Arith. >>

– 14 – , F’02 short int x = 15213; unsigned short int ux = (unsigned short) x; short int y = ; unsigned short int uy = (unsigned short) y; Casting Signed to Unsigned C Allows Conversions from Signed to Unsigned Resulting Value No change in bit representation Nonnegative values unchanged ux = Negative values change into (large) positive values uy = 50323

– 15 – , F’02 Signed vs. Unsigned in C Constants By default are considered to be signed integers Unsigned if have “U” as suffix 0U, UCasting Explicit casting between signed & unsigned same as U2T and T2U int tx, ty; unsigned ux, uy; tx = (int) ux; uy = (unsigned) ty; Implicit casting also occurs via assignments and procedure calls tx = ux; uy = ty;

– 16 – , F’02 00U== unsigned -10< signed -10U> unsigned > signed U < unsigned -1-2 > signed (unsigned) -1-2 > unsigned U < unsigned (int) U> signed Casting Surprises Expression Evaluation If mix unsigned and signed in single expression, signed values implicitly cast to unsigned Including comparison operations, ==, = Examples for W = 32 Constant 1 Constant 2 RelationEvaluation 00U U U (unsigned) U (int) U

– 17 – , F’02 0 TMax TMin –1 –2 0 UMax UMax – 1 TMax TMax + 1 2’s Comp. Range Unsigned Range Explanation of Casting Surprises 2’s Comp.  Unsigned Ordering Inversion Negative  Big Positive

– 18 – , F’02 Power-of-2 Multiply with Shift Operation u << k gives u * 2 k Both signed and unsignedExamples u << 3==u * 8 u << 5 - u << 3==u * 24 Most machines shift and add much faster than multiply Compiler generates this code automatically u 2k2k * u · 2 k True Product: w+k bits Operands: w bits Discard k bits: w bits UMult w (u, 2 k ) k 000 TMult w (u, 2 k ) 000

– 19 – , F’02 Unsigned Power-of-2 Divide with Shift Quotient of Unsigned by Power of 2 u >> k gives  u / 2 k  Uses logical shift u 2k2k / u / 2 k Division: Operands: k 0  u / 2 k  Result:. Binary Point 0

– 20 – , F’02 Fractional Binary Numbers Representation Bits to right of “binary point” represent fractional powers of 2 Represents rational number: bibi b i–1 b2b2 b1b1 b0b0 b –1 b –2 b –3 b–jb–j i–1 2i2i 1/2 1/4 1/8 2–j2–j

– 21 – , F’02 Representable Numbers Limitation Can only exactly represent numbers of the form x/2 k Other numbers have repeating bit representations ValueRepresentation 1/ [01]… 2 1/ [0011]… 2 1/ [0011]… 2

– 22 – , F’02 Numerical Form – 1 s M 2 E Sign bit s determines whether number is negative or positive Significand M normally a fractional value in range [1.0,2.0). Exponent E weights value by power of twoEncoding MSB is sign bit exp field encodes E frac field encodes M Floating Point Representation sexpfrac

– 23 – , F’02 Encoding MSB is sign bit exp field encodes E frac field encodes MSizes Single precision: 8 exp bits, 23 frac bits 32 bits total Double precision: 11 exp bits, 52 frac bits 64 bits total Extended precision: 15 exp bits, 63 frac bits Only found in Intel-compatible machines Stored in 80 bits »1 bit wasted Floating Point Precisions sexpfrac

– 24 – , F’02 Special Values Condition exp = 111 … 1Cases exp = 111 … 1, frac = 000 … 0 Represents value   (infinity) Operation that overflows Both positive and negative E.g., 1.0/0.0 =  1.0/  0.0 = + , 1.0/  0.0 =   exp = 111 … 1, frac  000 … 0 Not-a-Number (NaN) Represents case when no numeric value can be determined E.g., sqrt(–1), 