Lecture 2: Bits, Bytes, Ints

Slides:



Advertisements
Similar presentations
CS 61C L02 Number Representation (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
Advertisements

IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
CSCE 212 Computer Organization Lecture 2 Data Representation.
CS 61C L02 Number Representation (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
ICS 2005 Instructor: Peter A. Dinda TA: Bin Lin Recitation 2.
Bit Operations C is well suited to system programming because it contains operators that can manipulate data at the bit level –Example: The Internet requires.
February 4, 2003 CSCE 212 Computer Architecture Lecture 3 Representing Integers.
Unsigned and Signed Numbers. Hexadecimal Number 217A 16 Position Digits A Value = 2x x x16 + Ax1 = 2x x x16.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
CS 270: Computer Organization Bits, Bytes, and Integers
Bits and Bytes Topics Representing information as bits Bit-level manipulations Boolean algebra Expressing in C.
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.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
CS1372: HELPING TO PUT THE COMPUTING IN ECE CS1372 Some Basics.
University of Washington Integers The Hardware/Software Interface CSE351 Winter 2013.
Bits and Bytes Boolean Algebra ( ) Boolean algebra Expressing in C.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.
1 Manipulating Information (1). 2 Outline Bit-level operations Suggested reading –2.1.7~
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
The Machine Model Memory
Today’s Instructor: Randy Bryant
Chap. 2. Types, Operators, and Expressions
Recitation 4&5 and review 1 & 2 & 3
Integers Topics Numeric Encodings (2.2) Programming Implications
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Bits, Bytes, and Integers CSE 238/2038/2138: Systems Programming
Morgan Kaufmann Publishers
University of Washington
Instructor: David Ferry
Section 2: Integer & Floating Point Numbers
Binary numbers and arithmetic
Bit Operations Horton pp
CISC/CMPE320 - Prof. McLeod
CS213 Integers Topics Numeric Encodings Programming Implications
Data Structures Mohammed Thajeel To the second year students
Representing Information
Data III & Integers I CSE 351 Autumn 2016
Data III & Integers I CSE 351 Winter 2017
Introduction to Abstract Data Types
Bits and Bytes Topics Representing information as bits
Computer Architecture & Operations I
Integers II CSE 410 Winter 2017 Instructor: Teaching Assistants:
Bits and Bytes Topics Representing information as bits
Bits and Bytes Boolean algebra Expressing in C
Bits and Bytes Topics Representing information as bits
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
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.
Memory, Data, & Addressing II CSE 351 Summer 2018
Bits and Bytes Topics Representing information as bits
Integers II CSE 351 Autumn 2018 Instructor: Teaching Assistants:
Bit-wise and bit-shift operators
Integers II CSE 351 Summer 2018 Instructor: Justin Hsia
Comp Org & Assembly Lang
Computer Organization COMP 210
Data III & Integers I CSE 351 Winter 2018
Comp Org & Assembly Lang
Data III & Integers I CSE 351 Winter 2018
Bitwise Operators.
Operations and Arithmetic
“The Class That Gives CMU Its Zip!”
“The Class That Gives CMU Its Zip!”
Integers II CSE 351 Spring 2019 Instructor: Teaching Assistants:
Bit Operations Horton pp
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

Lecture 2: Bits, Bytes, Ints CS 105 January 28, 2019

The C Language Data and execution model are “closer to the machine” Syntax like Java: declarations, if, while, return Data and execution model are “closer to the machine” More power and flexibility More ways to make mistakes Sometimes confusing relationships Pointers!! A possible resource from CMU: http://www.cs.cmu.edu/afs/cs/academic/class/15213-s16/www/recitations/c_boot_camp.pdf

Memory: A (very large) array of bytes An index into the array is an address, location, or pointer Often expressed in hexadecimal We speak of the value in memory at an address The value may be a single byte … … or a multi-byte quantity starting at that address Larger words (32- or 64-bit) are stored in contiguous bytes The address of a word is the address of its first byte Successive addresses differ by word size Assignment: x = y + 42; Take the value from the location reserved for y Add 42 to it Place the result in the location reserved for x Walk through A1 Part 1 in terms of memory -> Endianess discussion

Endianness

Boolean Algebra Developed by George Boole in 19th Century Algebraic representation of logic---encode “True” as 1 and “False” as 0 And Or Not Exclusive-Or (Xor)

General Boolean algebras Bitwise operations on words How does this map to set operations? 01101001 & 01010101 01000001 01101001 | 01010101 01111101 01101001 ^ 01010101 00111100 ~ 01010101 10101010 01000001 01111101 00111100 10101010

Practice with Boolean algebras Assume: a = 01101001, b = 01010101 What are the results of evaluating the following Boolean operations? ~a ~b a & b a | b a ^ b

Bitwise vs Logical Operations in C Apply to any “integral” data type int, unsigned, long, short, char Bitwise Operators &, |, ~, ^ View arguments as bit vectors operations applied bit-wise in parallel Logical Operators &&, ||, ! View 0 as “False” View anything nonzero as “True” Always return 0 or 1 Early termination

Bitwise vs Logical Operations in C Exercises (char data type, one byte) ~0x41 ~0x00 ~~0x41 0x69 & 0x55 0x69 | 0x55 !0x41 !0x00 !!0x41 0x69 && 0x55 0x69 || 0x55 0xBE, 0xFF, 0x41 0x41, 0x7D 0x00, 0x01, 0x01 0x01, 0x01

Bit Shifting 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 left Undefined Behavior if you shift amount < 0 or ≥ word size Choice between logical and arithmetic depends on the type of data

Bit Shifting 0x41 << 4 0x41 >> 4 41 << 4 -41 << 4 -41 >> 4

Representing Unsigned Integers Think of bits as the binary representation If you have w bits, what is the range? Can only represent non-negative numbers

Representing Signed Numbers Option 1: sign-magnitude One bit for sign; interpret rest as magnitude Option 2: excess-K Choose a positive K in the middle of the unsigned range SignedValue(w) = UnsignedValue(w) – K Option 3: one’s complement Flip every bit to get the negation

Representing Signed Integers Option 4: two’s complement Most commonly used Like unsigned, except the high-order contribution is negative Assume C short (2 bytes) What is the hex/binary representation for 47? What is the hex/binary representation for -47? 47 = 0x2F = 0b0000 0000 0010 1111 -47 = oxFFD1 = 0b1111 1111 1101 0001

Example: Three-bit integers

Two’s Complement Signed Integers “Signed” does not mean “negative” High order bit is the sign bit To negate, complement all the bits and add 1 Remember the arithmetic right shift Sign extension Arithmetic is the same as unsigned—same circuitry Error conditions and comparisons are different -1 is all 1’s Sign extension

Unsigned and Signed Integers Use w-bit words; w can be 8, 16, 32, or 64 The bit sequence bw-1 … b1 b0 represents an integer Important!! ”signed” does not mean “negative”

Important Signed Numbers 8 16 32 64 TMax 0x7F 0x7FFF 0x7FFFFFFF 0x7FFFFFFFFFFFFFFF TMin 0x80 0x8000 0x80000000 0x8000000000000000 0x00 0x0000 0x00000000 0x0000000000000000 -1 0xFF 0xFFFF 0xFFFFFFFF 0xFFFFFFFFFFFFFFFF

Fun with Integers: Using Bitwise Operations x & 1 (x + 7) & 0xFFFFFFF8 p & ~0x3FF ((p >> 10) << 10) p & 0x3FF “x is odd” “round up to a multiple of 8” “start of 1K block containing p” (ish) same location (really) “offset of p within the block”