Homework Homework Continue Reading K&R Chapter 2 Questions?

Slides:



Advertisements
Similar presentations
Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS
Advertisements

Computer Structure - Computer Arithmetic Goal: Representing Numbers in Binary  Base 10 (decimal) - Numbers are represented using 10 numerals: 0, 1, 2,
1 Representing Numbers Using Bases Numbers in base 10 are called decimal numbers, they are composed of 10 numerals ( ספרות ) = 9* * *10.
Assembly Language and Computer Architecture Using C++ and Java

1 Binary Arithmetic, Subtraction The rules for binary arithmetic are: = 0, carry = = 1, carry = = 1, carry = = 0, carry =
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
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.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
Professor Jennifer Rexford COS 217
1 Binary Numbers Again Recall that N binary digits (N bits) can represent unsigned integers from 0 to 2 N bits = 0 to 15 8 bits = 0 to bits.
Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University See: P&H Chapter 2.4, 3.2, B.2, B.5, B.6.
Computers Organization & Assembly Language
CMPE 325 Computer Architecture II Cem Ergün Eastern Mediterranean University Integer Representation and the ALU.
1 Homework Turn in HW2 tonight HW3 is on-line already Questions?
Lec 3: Data Representation Computer Organization & Assembly Language Programming.
10-Sep Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept Representing Information in Computers:  numbers: counting numbers,
Number Systems. Today Decimal Hexadecimal Binary –Unsigned Binary –1’s Complement Binary –2’s Complement Binary.
1 CS103 Guest Lecture Number Systems & Conversion Bitwise Logic Operations.
BR 8/99 Binary Numbers Again Recall than N binary digits (N bits) can represent unsigned integers from 0 to 2 N bits = 0 to 15 8 bits = 0 to 255.
Number Systems Binary to Decimal Octal to Decimal Hexadecimal to Decimal Binary to Octal Binary to Hexadecimal Two’s Complement.
Bit Operations Horton pp Why we need to work with bits Sometimes one bit is enough to store your data: say the gender of the student (e.g. 0.
1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?
Digital Representations ME 4611 Binary Representation Only two states (0 and 1) Easy to implement electronically %0= (0) 10 %1= (1) 10 %10= (2) 10 %11=
Starting Chapter 2 K&R. Read ahead.. Call by Value vs Call by Reference Simple variables passed as function parameters in C are actually only copies on.
Number Representation and Arithmetic Circuits
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Number Systems and Computer Arithmetic Winter 2014 COMP 1380 Discrete Structures I Computing Science Thompson Rivers University.
Chapter 4 Operations on Bits. Apply arithmetic operations on bits when the integer is represented in two’s complement. Apply logical operations on bits.
Data Representation. Representation of data in a computer Two conditions: 1. Presence of a voltage – “1” 2. Absence of a voltage – “0”
ECE 2110: Introduction to Digital Systems Signed Number Conversions.
William Stallings Computer Organization and Architecture 8th Edition
Data Representation COE 308 Computer Architecture
Addition and Subtraction
Data Representation ICS 233
Lec 3: Data Representation
Arithmetic Shifts and Character Representation
Data Representation.
Chapter 4 Operations on Bits.
Chap. 2. Types, Operators, and Expressions
Data Representation Binary Numbers Binary Addition
Integer Real Numbers Character Boolean Memory Address CPU Data Types
CHAPTER 1 : INTRODUCTION
Boolean Algebra, Bitwise Operations
Instructor: David Ferry
Bit Operations Horton pp
Number Systems and Bitwise Operation
University of Gujrat Department of Computer Science
What to bring: iCard, pens/pencils (They provide the scratch paper)
Bit-Level Discussion of Numeric Types
Data Representation COE 301 Computer Organization
ECEG-3202 Computer Architecture and Organization
1 The Hardware/Software Interface CSE351 Spring 2011 Module 3: Integers Monday, April 4, 2011.
Integers II CSE 351 Summer 2018 Instructor: Justin Hsia
Number Systems Rayat Shikshan Sanstha’s
EECE.2160 ECE Application Programming
Comp Org & Assembly Lang
Number Systems Rayat Shikshan Sanstha’s
Data Representation ICS 233
ECE 331 – Digital System Design
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
GCSE COMPUTER SCIENCE Topic 3 - Data 3.3 Logical and Arithmetic Shifts.
靜夜思 床前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。 ~ 李白 李商隱.
MIPS Arithmetic and Logic Instructions
Bit Manipulations CS212.
COE 202: Digital Logic Design Number Systems Part 2
Bit Operations Horton pp
Data Representation COE 308 Computer Architecture
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

Homework Homework Continue Reading K&R Chapter 2 Questions?

Converting Decimal to Binary Divide the decimal number successively by 2 and write down the remainder in binary form: e.g. 11710 117 1 LSB (after divide by 2, remainder =1) 58 0 (after divide by 2, remainder =0) 29 1 (after divide by 2, remainder =1) 14 0 (after divide by 2, remainder =0) 7 1 (after divide by 2, remainder =1) 3 1 (after divide by 2, remainder =1) 1 1 (after divide by 2, remainder =1) 0 0 MSB Read UP and add any leading 0’s: 01110101 odd even Quotient

Converting Decimal to Hex Method 1: Convert the decimal number to binary and group the binary digits in groups of 4 e.g. 11710  0111 01012  7516 Method 2 (shown in HW3): Divide the decimal number successively by 16 and write down the remainder in hex form: 117 5 LSB (after divide by 16, remainder =5) 7 7 MSB (after divide by 16, remainder =7) Read UP and add any leading 0’s: 0x75

Converting Binary to Decimal Treat each bit position n that contains a one as adding 2n to the value. Ignore zeros because they can’t add anything to the value. Bit 0 LSB 1 1 (= 20) Bit 1 0 0 Bit 2 1 4 (= 22) Bit 3 1 8 (= 23) Bit 4 0 0 Bit 5 1 32 (= 25) Bit 6 0 0 Bit 7 MSB 0 0 Total 45 1011012 = (20*1) + (21*0) + (22*1) + (23*1) + (24*0) + (25*1) = 45

Converting Hex to Decimal Treat each digit n as adding 16n to the value. Ignore zeros because they can’t add anything to the value. Digit 0 LSB 0 0 Digit 1 2 32 (= 2 * 161) Digit 2 b 2816 (= 11 * 162) Digit 3 0 0 Digit 4 0 0 Digit 5 1 1048576 (= 1 * 165) Digit 6 0 0 Digit 7 MSB 0 0 Total 1051424

Converting base x to Decimal (…abcde)x = (x0*e) + (x1*d) + (x2*c) + (x3*b) + (x4*a) + . . . . . . x4 x3 x2 x1 x0

Base for Integer Variable Designating the base for an integer variable If variable begins with either: 0x It is Hex with a-f as Hex digits 0X It is Hex with A-F as Hex digits Otherwise, if variable begins with 0 It is Octal Otherwise, it is decimal Just a convention

Base for Character variable Designating the base for a character variable If variable is like either: ‘\x . . .’ It is Hex with 0-9 and a-f as Hex digits ‘\X . . .’ It is Hex with 0-9 and A-F as Hex digits Otherwise, if variable is like ‘\0 . . .’ It is Octal Otherwise, it is the ASCII code for a character ‘a’

Variables Character and Integer variables are Signed! Sign bit (MSB) not set ‘\x55’ equals an 8-bit quantity 01010101 when casted to an int, it equals 0000 … 01010101 0x55 equals a 32-bit quantity 0000 … 01010101 Sign bit (MSB) set ‘\xaa’ equals an 8-bit quantity 10101010 when casted to an int, it equals 1111 … 10101010 0xaa equals a 32 bit quantity 0000 … 10101010 Note: None of the ASCII codes (x00 - x7f) have sign bit set! Char b; int c; b = 0x55; c = b; All the previous numbers extend to 1

Signed (Default) Behavior Careful mixing modes when initializing variables! int i; (signed behavior is default) char c; (signed behavior is default) i = 0xaa; (== 000000aa) as intended i = ‘\xaa’; (== ffff ffaa) sign extends! c = ‘\xaa’; (== aa) as intended i = c; (==ffff ffaa) sign extends! Sign Bit Extension char 1 0101010 int 11111111 11111111 11111111 1 0101010

Unsigned Behavior Careful mixing modes when initializing variables! unsigned int i; (must specify unsigned if wanted) unsigned char c; (must specify unsigned if wanted) i = 0xaa; (== 000000aa) as intended i = ‘\xaa’; (== ffffffaa) char sign extends! c = ‘\xaa’; (== aa) as intended i = c; (==0000 00aa) char sign not extended! Sign Bit Extension char 1 0101010 int 00000000 00000000 00000000 1 0101010

Example to illustrate signed/unsigned types void copy_characters(void){ char ch; while ((ch =getchar()) != EOF) putchar(ch); } What happens if ch is defined as unsigned char? Is this true? EOF -> 32 bits 1 If getchar() == EOF, is this true? No yes Changing the declaration of ch into int ch; makes it work.

One’s Complement Two’s Complement One’s Complement Character Arithmetic ~ is the one’s complement operator ~ flips the value of each bit in the data All zeroes become one, All ones become zero ~ ‘\xaa’ == ‘\x55’ ~10101010 == 01010101 Number anded with its one’s complement = 0 10101010 & 01010101 00000000

One’s Complement Two’s Complement Two’s Complement Character Arithmetic - is the two’s complement operator flips the value of each bit in the data and adds 1 It creates the negative of the data value - ‘\x55’ == ‘\xab’ - 01010101 == 10101011 Number added to its two’s complement = 0 01010101 + 10101011 (1) 00000000 (carry out of MSB is dropped) Show an example 0101 1110 You can not just flip the MSB and get the negative number. You should get the 2’s complement of the number The computers do the same thing when you want to subtract two numbers

Two Special Case Values char 0 (or zero of any length) -00000000 = 11111111 + 1 = 00000000 char -27 (or -2n-1 for any length = n) -10000000 = 01111111 = 10000000 -128 to 127. the number is bigger than you can represent it here. overflow

Bit Manipulation Bitwise Operators: ~ one’s complement (unary not) & and | or ^ xor (exclusive or) << left shift >> right shift Lots of possible Exam questions here!

Binary Logic Tables AND 1 OR 1 1 NOT 1 1 1 1 1 1 1 XOR 1 ADD 1 1 1 1 1 1 OR 1 1 NOT 1 1 1 1 1 1 1 XOR 1 ADD 1 ^ -> XOR -> Exclusive OR 1 1 Carry 1 1 1 1 1 Operands Results

Bit Manipulation unsigned char n = ‘\xa6’; n 10100110 ~n 01011001 (1s complement: flip bits) n | ‘\x65’ 10100110 turn on bit in result if | 01100101 on in either operand 11100111

Bit Manipulations n & ‘\x65’ 10100110 turn on bit in result if & 01100101 on in both operands 00100100 n ^ ‘\x65’ 10100110 turn on bit in result if ^ 01100101 on in exactly 1 operand 11000011

Bit Manipulations n = ‘\x18’; 00011000 (Remember n is unsigned) n << 1 00110000 shift 1 to left (like times 2) n << 2 01100000 shift 2 to left (like times 4) n << 4 10000000 shift 4 to left (bits disappear off left end) n >> 2 00000110 shift 2 to right (like / 4) n >> 4 00000001 shift 4 to right (bits disappear off right end) Shift left -> multiply by 2 Unsigned Right Shift 0

Bit Manipulations Right shift result may be different if n is signed! If value of n has sign bit = 0, works same as last slide If value of n has sign bit = 1, works differently!! char n = ‘\xa5’; (default is signed) n 10100101 (sign bit is set) n >>2 11101001 (bring in 1’s from left) Bringing in extra copies of the sign bit on the left is also called "sign extension" Signed Right Shift

Bit Manipulations For signed variable, negative value shifted right by 2 or 4 is still a negative value ‘\xa5’ =10100101 ‘\xa5’ >> 2 = 11101001 = ‘\xe9’ Same result as divide by 4 (22 = 4) But, this is not true on all machines See K&R pg. 49, lines 8-10.

Bit Manipulations When dividing a negative value Note that (-1)/2 = -1 Different rounding rules than for positive value Remainder must be negative - not positive Note that (-1)/2 = -1 Note that -(1/2) = 0 Show -1 Round down