Bit-Level Discussion of Numeric Types

Slides:



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

Working with the data type: char  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
#1 Lec # 2 Winter EECC341 - Shaaban Positional Number Systems A number system consists of an order set of symbols (digits) with relations.
1 Homework Turn in HW2 tonight HW3 is on-line already Questions?
1 CS103 Guest Lecture Number Systems & Conversion Bitwise Logic Operations.
Chapter 2 Number Systems: Decimal, Binary, and Hex.
Data Representation in Computer Systems. 2 Signed Integer Representation The conversions we have so far presented have involved only positive numbers.
07/12/ Data Representation Two’s Complement & Binary Arithmetic.
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=
Introduction To Number Systems Binary System M. AL-Towaileb1.
Road map char data type Reading –Liang 5: Chapter 2: 2.7.4; 2.9; –Liang 6: Chapter 2: 2.7.4; 2.9 –Liang 7: Chapter 2: 2.7.4; 2.9.
Binary Addition The simplest arithmetic operation in binary is addition. Adding two single-digit binary numbers is relatively simple, using a form of carrying:
Data Representation COE 308 Computer Architecture
Cosc 2150: Computer Organization
Design of Digital Circuits Reading: Binary Numbers
Computer Architecture & Operations I
Addition and Subtraction
Data Representation ICS 233
Data Representation.
Negative Binary Numbers
2's Complement Arithmetic
Number Systems and Binary Arithmetic
CHAPTER 1 : INTRODUCTION
Bits, Bytes, and Integers CSE 238/2038/2138: Systems Programming
Digital Arithmetic Wen-Hung Liao, Ph.D..
University of Washington
CSCI206 - Computer Organization & Programming
Chapter 3 - Operators Arithmetic Operators Assignment
Computer Architecture & Operations I
Lecture 2 Topics Binary Arithmetic (Unsigned binary operands)
Negative Binary Numbers
ECE 103 Engineering Programming Chapter 3 Numbers
Integer Representations and Arithmetic
Bit Operations Horton pp
Number Systems and Bitwise Operation
IT 0213: INTRODUCTION TO COMPUTER ARCHITECTURE
Lecture 2 Topics Binary Arithmetic (Unsigned binary operands)
What to bring: iCard, pens/pencils (They provide the scratch paper)
CS 240 – Lecture 8 Bitwise Operations, Bit Manipulation, Type Conversion, Conditional Expression.
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Chapter 1 C for Embedded Systems.
MMNSS COLLEGE,KOTTIYAM DEPARTMENT OF PHYSICS
CSCI206 - Computer Organization & Programming
Chapter 1 C for Embedded Systems.
Data Representation in Computer Systems
2’s Complement Arithmetic
Digital Logic Design (ECEg3141) 2. Number systems, operations & codes 1.
1 The Hardware/Software Interface CSE351 Spring 2011 Module 3: Integers Monday, April 4, 2011.
COMS 161 Introduction to Computing
C1 Number systems.
Binary “There are 10 types of people in the world: Those who understand binary, and those who don't.”
Homework Homework Continue Reading K&R Chapter 2 Questions?
Number Systems Rayat Shikshan Sanstha’s
Number Systems Rayat Shikshan Sanstha’s
Binary to Decimal Conversion
ECE 171 Digital Circuits Chapter 2 Binary Arithmetic
Data Types and Expressions
COMS 161 Introduction to Computing
Lecture No.5.
COMS 361 Computer Organization
Chapter3 Fixed Point Representation
Chapter 1 (Part c) Digital Systems and Binary Numbers
Introduction To Number Systems
Bit Operations Horton pp
Data Representation COE 308 Computer Architecture
Two’s Complement & Binary Arithmetic
ECE 120 Midterm 1 HKN Review Session.
Today Binary addition Representing negative numbers 2.
Presentation transcript:

Bit-Level Discussion of Numeric Types Homework 2 has been extended to October 4th, 2017 Finish reading Chapter 2 of Kernigan and Ritchie

Review of char and int types char type 8 bits, 1 byte Example: int type 32 bits, 4 bytes 0101 1101 '\x5d' 93 ']' 01101010 11001110 10010111 00011000 0x6ace9718 1791923992

Numeric Literals signed char literals signed int literals Example: Will be treated as an r-value of type signed char signed int literals Will be treated as an r-value of type signed int '\x##' 0x########

Explicit Casting Casting is the act of converting an r-value from one type to another. In most cases, this results in a change in bit-length of the value. Example: In memory: Another example: Note, large  small drops bits 0101 0101 char c = '\x55'; int i = (int) c; 00000000 00000000 00000000 0101 0101 int i = 0x7fff9876; char c = (char) i; 01111111 1111111 10011000 0111 0110 0111 0110

Implicit Casting Just like Explicit Casting, but doesn't use the (type) operator. Example: The value of variable c must be converted to type (int) before arithmetic can be done between it and the value of variable i. char c = 100; int i = 100; printf("%d\n", c * i);

Negative Numbers To describe negative numbers, we need to make use of a property of binary arithmetic. Let's do this by first defining our first negative number: -1 Negative one (-1) is the number to which adding one (+1) will give us zero (0). There is only one such number: Note, adding +1 to that number actually gives us but we can't retain the carry. XXXX XXXX + 0000 0001 0000 0000 1111 1111 1 0000 0000

One's Complement So, to define negative numbers in general, we need a few steps. First, we'll define the One's Complement operator (~): For any number, take it's binary representation and flip/toggle each bit. Examples: ~0000 0000 1111 1111 ~1110 0110 0001 1001 ~0x41100531 0xbeefface ~0x35014541 0xcafebabe

Two's Complement Using the One's Complement (~) we can define the Two's Complement as something we're familiar with: The Negative operator (–) The Two's Complement is equivalent to the One's Complement Plus One. Example: ~ 1010 1001 0101 0110 1010 1001 = -87 0101 0111 = 87 ~ a9 56 0101 0110 + 0000 0001 0101 0111 56 + 01 57 \xa9 = -87 \x57 = 87

Fast One's Complement in Hex Due to the fact that hex digits are just groups of four binary digits, we can perform One's Complement on them as a grouping. To perform One's Complement this way, switch hex digits (groups of 4 bits) horizontally. f 1 e 2 d 3 c 4 b 5 a 6 9 7 8 0000 1111 0001 1110 0010 1101 0011 1100 0100 1011 0101 1010 0110 1001 0111 1000

Casting Revisited (with Negative Numbers) Now that we know how to represent negative numbers, there's an important behavior to describe in casting. The left-most bit of a numeric type is the Most Significant Bit (MSB) This bit corresponds to the sign of a number (positive or negative, +/–) When casting from a smaller signed type to a larger signed type, the sign expands across all added bits. Example: (int) 1 101 0101 11111111 11111111 11111111 1 101 0101 Note: This still corresponds to the same negative number!