1 Homework Turn in HW2 tonight HW3 is on-line already Questions?

Slides:



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

Computer Organization & Assembly Language
Number Systems Discussion D4.1 Appendix C. Number Systems Counting in Binary Positional Notation Hexadecimal Numbers Negative Numbers.
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
Fixed-Point Arithmetics: Part I
Number Systems Standard positional representation of numbers:
Review Two’s complement

Assembly Language and Computer Architecture Using C++ and Java
Storage of Bits Computers represent information as patterns of bits
Computer ArchitectureFall 2008 © August 25, CS 447 – Computer Architecture Lecture 3 Computer Arithmetic (1)
1 Binary Arithmetic, Subtraction The rules for binary arithmetic are: = 0, carry = = 1, carry = = 1, carry = = 0, carry =
Negative Numbers Module M3.3 Section 2.4. Negative Numbers Subtract by adding ’s complement Ignore carry.
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.
Binary Operations Math/Logical. Binary Math Decimal Addition Example ) Add = 15 Write down 5, carry ) Add 3 +
Number Systems and Codes Discussion D4.1. Number Systems Counting in Binary Positional Notation Hexadecimal Numbers Negative Numbers.
S. Barua – CPSC 240 CHAPTER 2 BITS, DATA TYPES, & OPERATIONS Topics to be covered are Number systems.
Computer ArchitectureFall 2007 © August 29, 2007 Karem Sakallah CS 447 – Computer Architecture.
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.
Binary Arithmetic Math For Computers.
Number Systems Lecture 02.
1 Lecture 2: Number Systems Binary numbers Base conversion Arithmetic Number systems  Sign and magnitude  Ones-complement  Twos-complement Binary-coded.
Binary Addition Addition Rules: = = = = = carry 1 1 carry 1 Example 1: Example 2:
ECE 2110: Introduction to Digital Systems Signed Number Conversions.
Arithmetic for Computers
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Integer Data representation Addition and Multiplication.
Numbering systems.
Number Systems Binary and Hexadecimal. Base 2 a.k.a. Binary  Binary works off of base of 2 instead of a base 10 like what we are taught in school 
Computer Arithmetic Nizamettin AYDIN
CMPE 325 Computer Architecture II Cem Ergün Eastern Mediterranean University Integer Representation and the ALU.
#1 Lec # 2 Winter EECC341 - Shaaban Positional Number Systems A number system consists of an order set of symbols (digits) with relations.
IT253: Computer Organization
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.
Information Representation. Digital Hardware Systems Digital Systems Digital vs. Analog Waveforms Analog: values vary over a broad range continuously.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
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.
Bit Manipulation when every bit counts. Questions on Bit Manipulation l what is the motivation for bit manipulation l what is the binary, hexadecimal,
ECE 301 – Digital Electronics Unsigned and Signed Numbers, Binary Arithmetic of Signed Numbers, and Binary Codes (Lecture #2)
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.
ECE 2110: Introduction to Digital Systems Signed Addition/Subtraction.
Number Systems Binary to Decimal Octal to Decimal Hexadecimal to Decimal Binary to Octal Binary to Hexadecimal Two’s Complement.
Number Systems and Logic Prepared by Dr P Marais (Modified by D Burford)
Ch3a- 2 EE/CS/CPE Computer Organization  Seattle Pacific University Crunching Numbers Topics we need to explore Representing numbers on a computer.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Number Systems and Bitwise Operation.
Introduction to Microprocessors Chapter 2. Decimal or Base 10 Numbers  Have ten different digits (0-9)  It is a weighted number system. Each position.
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.
Arithmetic Operations
Number Representation and Arithmetic Circuits
ECE 171 Digital Circuits Chapter 2 Binary Arithmetic Herbert G. Mayer, PSU Status 1/14/2016 Copied with Permission from prof. Mark PSU ECE.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
09/03/20161 Information Representation Two’s Complement & Binary Arithmetic.
Nguyen Le CS147.  2.4 Signed Integer Representation  – Signed Magnitude  – Complement Systems  – Unsigned Versus Signed Numbers.
ECE 3110: Introduction to Digital Systems Signed Number Conversions and operations.
Binary Addition The simplest arithmetic operation in binary is addition. Adding two single-digit binary numbers is relatively simple, using a form of carrying:
Advanced Binary b. describe and use two’s complement and sign and magnitude to represent negative integers; c. perform integer binary arithmetic, that.
ECE 2110: Introduction to Digital Systems Signed Number Conversions.
Number Systems and Bitwise Operation
University of Gujrat Department of Computer Science
What to bring: iCard, pens/pencils (They provide the scratch paper)
1 The Hardware/Software Interface CSE351 Spring 2011 Module 3: Integers Monday, April 4, 2011.
Homework Homework Continue Reading K&R Chapter 2 Questions?
Integers II CSE 351 Summer 2018 Instructor: Justin Hsia
Bitwise Operators.
Bit Manipulations CS212.
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

1 Homework Turn in HW2 tonight HW3 is on-line already Questions?

2 Base for Integer Constants Designating the base for an integer constant If constant begins with either: 0x It is Hex with a-f as Hex digits 0XIt is Hex with A-F as Hex digits Otherwise, if constant begins with 0It is Octal Otherwise, it is decimal 23Decimal 23

3 Base for Character Constants Designating the base for a character constant If constant begins with either: ‘\x It is Hex with a-f as Hex digits ‘\XIt is Hex with A-F as Hex digits Otherwise, if constant begins with ‘\0It is Octal Otherwise, it is the ASCII code for a character ‘a’ASCII code for lower case character a

4 Signed/Unsigned Constants Constants are Signed by default! Important when converted to 32 bits of significance –When sign bit (MSB) is equal to 0: 0x55 as a 32 bit quantity 0000 … ‘\x55’ as a 32 bit quantity 0000 … When sign bit (MSB) is equal to 1: 0xaa as a 32 bit quantity 0000 … ‘\xaa’ as a 32 bit quantity 1111 …

5 What we’d expect to see! C Source Code int i = 0x55; /* int set = to int constant */ int j = '\x55'; /* int set = to char constant */ printf("i= %x, j= %x\n", i, j); Executes and prints as i= 55, j= 55

6 What Happened? C Source code int i = 0xaa;/* int set = to int constant */ int j = '\xaa'; /* int set = to char constant */ printf("i= %x, j= %x\n", i, j); Executes and prints: i= aa, j= ffffffaa

7 Signed (Default) Behavior Careful mixing modes when initializing variables! int i = 0xaa; (= aa) probably as intended int j = ‘\xaa’;(= ff ff ff aa) sign bit extension! char int Sign Bit Extension char int Sign Bit Extension

8 Signed (Default) Behavior Careful mixing modes when initializing variables! c = 0x ;(= ‘\x89’) truncates MSBs char int NOTE: char value is negative even though int was positive +

9 Unsigned Behavior C Source Code unsigned int i = 0xaa; unsigned int j = '\xaa'; printf("i= %x, j= %x\n", i, j); Executes and prints as: i= aa, j= aa

10 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’ ~ ==

11 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’ ==

12 Two Special Case Values char 0 (or zero of any length) = = char -2 7 (or -2 n-1 for any length = n) = =

13 Bit Manipulation Bitwise Operators: ~ unary not & and | or ^ xor (exclusive or) << leftshift >> rightshift Operate on pair of bits of the input data values!

14 Binary Logic Tables & ~ | Carry ^ NOT ANDOR XORADD

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

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

17 Bit Manipulations (Left Shift) char n = ‘\x18’; n << shift 1 to left (like * 2) n << shift 2 to left (like * 4) (MSBs disappear off left end) n can be either signed or unsigned – no difference Significance can be lost if data shifted too far left Left Shift0

18 Bit Manipulations (Right Shift) unsigned char n = ‘\x18’; n >> shift 1 to right (like / 2) n >> shift 2 to right (like / 4) (LSBs disappear off right end) Integer truncation of divide by 2 n just like with / Unsigned Right Shift 0

19 Bit Manipulations (Right Shift) signed char n = ‘\xa5’; n >> (bring in a 1 from the left) n >> (bring in two 1’s from left) The rule is to bring in a copy of the sign bit! Bringing in extra copies of the sign bit from the left end is another form of "sign extension" Signed Right Shift

20 Bit Manipulations (Right Shift) For a signed variable, negative value shifted right by 2 is still a negative value ‘\xa5’ == ‘\xa5’ >> 2 == == ‘\xe9’ Same result as divide by 4 (2 2 = 4)

21 Bit Manipulations (Right Shift) When right shifting a signed value: Different rounding results for positive vs negative Result stays negative – does not become positive 1>>1 = 0 Rounds down -(1>>1) = 0 Rounds down then applies minus (-1)>>1 = -1Applies minus then rounds down -1>>1 = -1Applies minus then rounds down Rounding behavior not exactly same as /2

22 Converting Decimal to Binary From get binary by halving the number successively (rounding down) and writing down the digit 1 when have an odd result, 0 when result is even (consider number itself the first result): 1171 LSB MSB Read UP and add any leading 0’s: = ‘\x75’

23 Unsigned Binary to Decimal Treat each bit position n that contains a one as adding 2 n to the value. Ignore zeros because they can’t add anything to the value. Bit 0LSB 1 1(= 2 0 ) Bit 10 0 Bit 21 4(= 2 2 ) Bit 31 8(= 2 3 ) Bit 40 0 Bit 5132(= 2 5 ) Bit 60 0 Bit 7MSB1 128(= 2 7 ) Total 173

24 Signed Binary to Decimal Treat each bit position n that contains a one as adding 2 n to the value except treat the MSB as subtracting 2 n. (Ignore zeros as before) Bit 0LSB 1 1(= 2 0 ) Bit 10 0 Bit 21 4(= 2 2 ) Bit 31 8(= 2 3 ) Bit 40 0 Bit 5132(= 2 5 ) Bit 60 0 Bit 7MSB1 -128(= -2 7 ) Total -83

25 Converting Hex to Decimal Treat each digit n as adding 16 n to the value. Ignore zeros because they can’t add anything to the value. Digit 0LSBs 0 0 Digit (= 2 * 16 1 ) Digit 2b 2816 (= 11 * 16 2 ) Digit 30 0 Digit 40 0 Digit (= 1 * 16 5 ) Digit 60 0 Digit 7MSBs0 0 Total