CSE 351 Number Representation. Number Bases Any numerical value can be represented as a linear combination of powers of n, where n is an integer greater.

Slides:



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

Number Bases Informatics INFO I101 February 9, 2004 John C. Paolillo, Instructor.
COE 202: Digital Logic Design Signed Numbers
Computer Structure - Computer Arithmetic Goal: Representing Numbers in Binary  Base 10 (decimal) - Numbers are represented using 10 numerals: 0, 1, 2,
King Fahd University of Petroleum and Minerals
Data Representation Computer Organization &
TK 2633 Microprocessor & Interfacing Lecture 3: Introduction to 8085 Assembly Language Programming (2) 1 Prepared By: Associate Prof. Dr Masri Ayob.
CS 151 Digital Systems Design Lecture 3 More Number Systems.
Data Representation COE 205
Assembly Language and Computer Architecture Using C++ and Java
Number Systems Standard positional representation of numbers:
Review Two’s complement

Assembly Language and Computer Architecture Using C++ and Java
Room: E-3-31 Phone: Dr Masri Ayob TK 2633 Microprocessor & Interfacing Lecture 1: Introduction to 8085 Assembly Language.
24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05.
Computer ArchitectureFall 2008 © August 25, CS 447 – Computer Architecture Lecture 3 Computer Arithmetic (1)
CSCE 211: Digital Logic Design Chin-Tser Huang University of South Carolina.
Data Representation ICS 233
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
ENGIN112 L3: More Number Systems September 8, 2003 ENGIN 112 Intro to Electrical and Computer Engineering Lecture 3 More 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.
Number Systems Lecture 02.
COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter.
Binary Representation and Computer Arithmetic
Simple Data Type Representation and conversion of numbers
Computers Organization & Assembly Language
CMPE 325 Computer Architecture II Cem Ergün Eastern Mediterranean University Integer Representation and the ALU.
Number Systems Part 2 Numerical Overflow Right and Left Shifts Storage Methods Subtraction Ranges.
Summer 2014 Chapter 1: Basic Concepts. Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, Chapter Overview Welcome to Assembly Language.
Click to edit Master title style Click to edit Master text styles –Second level Third level –Fourth level »Fifth level 1 Today’s Topics How information.
Logic and Digital System Design - CS 303
IT253: Computer Organization
ECEN2102 Digital Logic Design Lecture 1 Numbers Systems Abdullah Said Alkalbani University of Buraimi.
Calculating Two’s Complement. The two's complement of a binary number is defined as the value obtained by subtracting the number from a large power of.
Engineering 1040: Mechanisms & Electric Circuits Spring 2014 Number Systems.
Positional Number Systems
Operations on Bits Arithmetic Operations Logic Operations
Computer Organization and Assembly Language Bitwise Operators.
1 Saint Louis University Arithmetic and Bitwise Operations on Binary Data CSCI 224 / ECE 317: Computer Architecture Instructor: Prof. Jason Fritts Slides.
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.
Number Systems and Logic Prepared by Dr P Marais (Modified by D Burford)
EEL 3801C EEL 3801 Part I Computing Basics. EEL 3801C Data Representation Digital computers are binary in nature. They operate only on 0’s and 1’s. Everything.
Data Representation in Computer Systems. 2 Signed Integer Representation The conversions we have so far presented have involved only positive numbers.
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=
CSE 351 Number Representation & Operators Section 2 October 8, 2015.
Data Representation COE 301 Computer Organization Dr. Muhamed Mudawar
BINARY SYSTEMS ENGR. KASHIF SHAHZAD 1. BINARY NUMBERS 1/2 Internally, information in digital systems is of binary form groups of bits (i.e. binary numbers)
Data Representation. Representation of data in a computer Two conditions: 1. Presence of a voltage – “1” 2. Absence of a voltage – “0”
CHAPTER 3 BINARY NUMBER SYSTEM. Computers are electronic machines which operate using binary logic. These devices use two different values to represent.
CompSci From bits to bytes to ints  At some level everything is stored as either a zero or a one  A bit is a binary digit a byte is a binary.
Binary Addition The simplest arithmetic operation in binary is addition. Adding two single-digit binary numbers is relatively simple, using a form of carrying:
Dr. ClincyLecture 2 Slide 1 CS Chapter 2 (1 of 5) Dr. Clincy Professor of CS Note: Do not study chapter 2’s appendix (the topics will be covered.
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.
From bits to bytes to ints
Bits, Bytes, and Integers CSE 238/2038/2138: Systems Programming
Data Representation in Computer Systems
Bit Operations Horton pp
BEE1244 Digital System and Electronics BEE1244 Digital System and Electronic Chapter 2 Number Systems.
Computer Architecture & Operations I
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 and Bytes Topics Representing information as bits
Number Representation & Operators
Comp Org & Assembly Lang
Bitwise Operators.
Bit Operations Horton pp
Presentation transcript:

CSE 351 Number Representation

Number Bases Any numerical value can be represented as a linear combination of powers of n, where n is an integer greater than 1 Example: decimal (n=10) Decimal numbers are just linear combinations of 1, 10, 100, 1000, etc 1234 = 1* * *10 + 4*1 We can also use the base n=2 (binary) or n=16 (hexadecimal)

Binary Numbers Each digit is either a 1 or a 0 Each digit corresponds to a power of 2 Why use binary? Easy to physically represent two states in memory, registers, across wires, etc High/Low voltage levels This can scale to much larger numbers by using more hardware to store more bits

Converting Binary Numbers To convert the decimal number d to binary, do the following: Compute (d % 2). This will give you the lowest-order bit Continue to divide d by 2, round down to the nearest integer, and compute (d % 2) for successive bits Example: Convert 25 to binary First bit: (25 % 2) = 1 Second bit: (12 % 2) = 0 Third bit: 6 % 2 = 0 Fourth bit: 3 % 2 = 1 Fifth bit: 1 % 2 = 1 Stop because we reached zero

Hexadecimal Numbers Same concept as decimal and binary, but the base is 16 Why use hexadecimal? Easy to convert between hex and binary Much more compact than binary

Converting Hexadecimal Numbers To convert a decimal number to hexadecimal, use the same technique we used for binary, but divide/mod by 16 instead of 2 Hexadecimal numbers have a prefix of “0x” Example: Convert 1234 to hexadecimal First digit: (1234 % 16) = / 16 = 77 Second digit: (77 % 16) = 13 = D 77 / 16 = 4 Third digit: 4 % 16 = 4 4 / 16 = 0 Stop because we reached zero Result: 0x4D2

Representing Signed Integers There are several ways to represent signed integers Sign & Magnitude Use 1 bit for the sign, remaining bits for magnitude Works OK, but there are 2 ways to represent zero (-0 and 0) Also, arithmetic is tricky Two’s Complement Similar to regular binary representation Highest bit has negative weight rather than positive Works well with arithmetic, only one way to represent zero

Two’s Complement This is an example of the range of numbers that can be represented by a 4- bit two’s complement number An n bit, two’s complement number can represent the range [-2^(n-1), 2^(n-1)) Note the asymmetry of this range about 0 Note what happens when you overflow If you still don’t understand it, speak up! Very confusing concept

Bitwise Operators NOT: ~ This will flip all bits in the operand AND: & This will perform a bitwise AND on every pair of bits OR: | This will perform a bitwise OR on every pair of bits XOR: ^ This will perform a bitwise XOR on every pair of bits SHIFT: > This will shift the bits right or left

Logical Operators NOT: ! Evaluates the entire operand, rather than each bit Produces a 1 if != 0, produces 0 otherwise AND: && Produces 1 if both operands are nonzero OR: || Produces 1 if either operand is nonzero

Common Operator Uses A double bang (!!) is useful when normalizing values to 0 or 1 Imitates Boolean types Shifts are useful for multiplying/dividing quickly Most multiplications are reduced to shifts when possible by GCC already When writing assembly routines, shifts will be more useful Shifts are also consistent for negative numbers (thanks to sign extension) DeMorgan’s Laws: ~(A|B) == (~A & ~B) ~(A&B) == (~A | ~B)

Masks These are usually strings of 1s that are used to isolate a subset of bits in an operand Example: the mask 0xFF will “mask” the first byte of an integer Once you have created a mask, you can shift it left or right Example: the mask 0xFF << 8 will “mask” the second byte of an integer You can apply a mask in different ways To set bits in x, you can do x = x | MASK To invert bits in x, you can do x = x ^ MASK To erase everything but the desired bits in x, do x = x & MASK

Application: Symmetric Encryption This is an example that shows how XOR can be used to encrypt data Say Alice wishes to communicate message M to Bob Let M be the bit string: 0b Both Alice and Bob have a secret cipher key C Let C be the bit string: 0b Alice sends Bob the encrypted message M’ = M ^ C M’ = 0b Bob applies C to M’ to retrieve M M’ ^ C = 0b XOR ciphers are not very secure by themselves, but the XOR operation is used in some modes of AES encryption

Application: Gray Codes Gray Codes encode numbers such that consecutive numbers only differ in their representations by 1 bit Useful when trying to transfer the output of a counter across clock domains using a multi- wire bus If each wire represents one binary digit, we want to ensure that when the counter increments, the voltage level changes only on one wire Let n be our counter output (n >> 1) ^ n will produce a gray coded version of n If we receive the gray code g, we need to convert it to n : This will produce the original output of the counter as a binary number for (int mask = g >> 1; mask != 0; mask >> 1) { g = g ^ mask; }

Application: Bit Packing Let’s say you have the values x, y, and z that take 3, 4, and 1 bit to represent, respectively If you store each using an int, that will result in 4*3 = 12 bytes of space In C, we can pack them all together to only take up 1 byte of space Note, our implementation will end up getting padded to 4 bytes by the compiler Download pack.c from the website to take a look at a simple bit packing example