CS 261 Fall Binary Information (convert to hex) Mike Lam, Professor

Similar documents
Topic Notes: Bits and Bytes and Numbers

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: Bits and Bytes and Numbers

Topic Notes: Bits and Bytes and Numbers

CS 261 Fall Machine and Assembly Code. Data Movement and Arithmetic. Mike Lam, Professor

Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2

Binghamton University. CS-220 Spring X86 Debug. Computer Systems Section 3.11

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 1: Basic Concepts. Chapter Overview. Welcome to Assembly Language

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

BINARY SYSTEM. Binary system is used in digital systems because it is:

Fundamentals of Programming

Goals for this Week. CSC 2400: Computer Systems. Bits, Bytes and Data Types. Binary number system. Finite representations of binary integers

Final Labs and Tutors

M1 Computers and Data

Number Systems. Decimal numbers. Binary numbers. Chapter 1 <1> 8's column. 1000's column. 2's column. 4's column

Course Schedule. CS 221 Computer Architecture. Week 3: Plan. I. Hexadecimals and Character Representations. Hexadecimal Representation

Hardware: Logical View

Fundamentals of Programming (C)

LOGIC DESIGN. Dr. Mahmoud Abo_elfetouh

Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2 Fall 2018

CSE351 Autumn 2012 Midterm Exam (5 Nov 2012)

ELECTRICAL AND COMPUTER ENGINEERING DEPARTMENT, OAKLAND UNIVERSITY ECE-2700: Digital Logic Design Winter Notes - Unit 4. hundreds.

Inf2C - Computer Systems Lecture 2 Data Representation

Page 1. Where Have We Been? Chapter 2 Representing and Manipulating Information. Why Don t Computers Use Base 10?

Bits and Bytes. Why bits? Representing information as bits Binary/Hexadecimal Byte representations» numbers» characters and strings» Instructions

ELECTRICAL AND COMPUTER ENGINEERING DEPARTMENT, OAKLAND UNIVERSITY ECE-278: Digital Logic Design Fall Notes - Unit 4. hundreds.

Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

1.1. INTRODUCTION 1.2. NUMBER SYSTEMS

Representation of Information

When an instruction is initially read from memory it goes to the Instruction register.

Harry H. Porter, 2006

Course overview. Computer Organization and Assembly Languages Yung-Yu Chuang 2006/09/18. with slides by Kip Irvine

MACHINE LEVEL REPRESENTATION OF DATA

Survey. Motivation 29.5 / 40 class is required

data within a computer system are stored in one of 2 physical states (hence the use of binary digits)

JAVA OPERATORS GENERAL

Dec Hex Bin ORG ; ZERO. Introduction To Computing

Intermediate Programming & Design (C++) Notation

DIGITAL SYSTEM DESIGN

Topics Power tends to corrupt; absolute power corrupts absolutely. Computer Organization CS Data Representation

ECOM 2325 Computer Organization and Assembly Language. Instructor: Ruba A.Salamah INTRODUCTION

Math 230 Assembly Programming (AKA Computer Organization) Spring 2008

ITEC 1011 Introduction to Information Technologies

Representing Information. Bit Juggling. - Representing information using bits - Number representations. - Some other bits - Chapters 1 and 2.3,2.

2010 Summer Answers [OS I]

Mark II Aiken Relay Calculator

Binary Logic (review)

EE292: Fundamentals of ECE

Integer Representation

Course overview. Computer Organization and Assembly Languages Yung-Yu Chuang 2007/09/17. with slides by Kip Irvine

But first, encode deck of cards. Integer Representation. Two possible representations. Two better representations WELLESLEY CS 240 9/8/15

Lecture 2: Number Systems

Bits and Bytes January 13, 2005

Digital Fundamentals

l l l l l l l Base 2; each digit is 0 or 1 l Each bit in place i has value 2 i l Binary representation is used in computers

CSE 351 Midterm - Winter 2017

COMP2611: Computer Organization. Data Representation

Number Systems. TA: Mamun. References: Lecture notes of Introduction to Information Technologies (ITEC 1011) by Dr Scott MacKenzie

Why Don t Computers Use Base 10? Lecture 2 Bits and Bytes. Binary Representations. Byte-Oriented Memory Organization. Base 10 Number Representation

Chapter Overview. Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 1: Basic Concepts. Printing this Slide Show

History of Computing. Ahmed Sallam 11/28/2014 1

Beginning C Programming for Engineers

Bits, Bytes, and Integers

The type of all data used in a C++ program must be specified

Decimal & Binary Representation Systems. Decimal & Binary Representation Systems

1. Introduction to Assembly Language

Chapter 2 Number System

World Inside a Computer is Binary

Bits, Bytes and Integers

Arithmetic and Bitwise Operations on Binary Data

Overview. ELEC2041 Microprocessors and Interfacing. Lecture 7: Number Systems - II. March 2006.

Read this before starting!

Computer Organization and Programming

Why Don t Computers Use Base 10? Lecture 2 Bits and Bytes. Binary Representations. Byte-Oriented Memory Organization. Base 10 Number Representation

CS2630: Computer Organization Homework 1 Bits, bytes, and memory organization Due January 25, 2017, 11:59pm

EE 109 Unit 6 Binary Arithmetic

Data Representation. DRAM uses a single capacitor to store and a transistor to select. SRAM typically uses 6 transistors.

srl - shift right logical - 0 enters from left, bit drops off right end note: little-endian bit notation msb lsb "b" for bit

2. Integers. 9 * celsius / Celsius to Fahrenheit. Precedence. (9 * celsius / 5) + 32

2. Integers. 9 * celsius / Celsius to Fahrenheit. Precedence. (9 * celsius / 5) + 32

Data Representation COE 301. Computer Organization Prof. Muhamed Mudawar

Chapter 2 Number Systems and Codes Dr. Xu

CHW 261: Logic Design

17. Instruction Sets: Characteristics and Functions

CS 107 Lecture 2: Bits and Bytes (continued)

UNIT 7A Data Representation: Numbers and Text. Digital Data

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

2. Integers. 9 * celsius / Celsius to Fahrenheit. Precedence. (9 * celsius / 5) + 32

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

Semester Transition Point. EE 109 Unit 11 Binary Arithmetic. Binary Arithmetic ARITHMETIC

CSCI 2212: Intermediate Programming / C Chapter 15

Number Systems Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur Number Representation

The type of all data used in a C (or C++) program must be specified

CS 261 Fall Mike Lam, Professor Integer Encodings

Arithmetic Operations

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

CSE 351 Midterm - Winter 2015 Solutions

ELEG3923 Microprocessor Ch.0 & Ch.1 Introduction to Microcontroller

Kinds Of Data CHAPTER 3 DATA REPRESENTATION. Numbers Are Different! Positional Number Systems. Text. Numbers. Other

DATA REPRESENTATION. By- Neha Tyagi PGT CS KV 5 Jaipur II Shift, Jaipur Region. Based on CBSE curriculum Class 11. Neha Tyagi, KV 5 Jaipur II Shift

Transcription:

CS 261 Fall 2018 Mike Lam, Professor 3735928559 (convert to hex) Binary Information

Binary information Topics Base conversions (bin/dec/hex) Data sizes Byte ordering Character and program encodings Bitwise operations

Core theme Information = Bits + Context

Why binary? Computers store information in binary encodings 1 bit is the simplest form of information (on / off) Minimizes storage and transmission errors To store more complicated information, use more bits However, we need context to understand them Data encodings provide context For the next two weeks, we will study encodings First, let s become comfortable working with binary

Base conversions Binary encoding is base-2: bit i represents the value 2i Bits typically written from most to least significant (i.e., 2 3 2 2 2 1 2 0 ) 1 = 1 = 0 2 3 + 0 2 2 + 0 2 1 + 1 2 0 = [0001] 5 = 4 + 1 = 0 2 3 + 1 2 2 + 0 2 1 + 1 2 0 = [0101] 11 = 8 + 2 + 1 = 1 2 3 + 0 2 2 + 1 2 1 + 1 2 0 = [1011] 15 = 8 + 4 + 2 + 1 = 1 2 3 + 1 2 2 + 1 2 1 + 1 2 0 = [1111] 1-1=0 5-4=1 1-1=0 11-8=3 3-2=1 1-1=0 15-8=7 7-4=3 3-2=1 1-1=0 Binary to decimal: Add up all the powers of two (memorize powers of two to make this go faster!) Decimal to binary: Find highest power of two and subtract to find the remainder Repeat above until the remainder is zero Every power of two become 1; all other bits are 0

Remainder system Quick method for decimal binary conversions Repeatedly divide decimal number by two until zero, keeping track of remainders (either 0 or 1) Read in reverse to get binary equivalent 11 5 r 1 2 r 1 => 1011 (8 + 2 + 1) 1 r 0 0 r 1

Base conversions Hexadecimal encoding is base-16 (often prefixed with 0x ) Converting between hex and binary is easy Each digit represents 4 bits; just substitute digit-by-digit or in groups of four! You should memorize these equivalences Dec Bin Hex 0 0000 0 1 0001 1 2 0010 2 3 0011 3 4 0100 4 5 0101 5 6 0110 6 7 0111 7 Dec Bin Hex 8 1000 8 9 1001 9 10 1010 A 11 1011 B 12 1100 C 13 1101 D 14 1110 E 15 1111 F

Fundamental data sizes 1 byte = 2 hex digits (= 2 nibbles!) = 8 bits (most significant) (least significant) 1 byte: 128 64 32 16 8 4 2 1 2 7 2 6 2 5 2 4 2 3 2 2 2 1 1 hex digit (Y) 1 hex digit (Z) 2 0 Value of byte 0xYZ is 16Y + Z Machine word = size of an address (i.e., the size of a pointer in C) Early computers used 16-bit addresses Could address 2 16 bytes = 64 KB Now 32-bit (4 bytes) or 64-bit (8 bytes) Can address 4GB or 16 EB Prefix Bin Dec Kilo 2 10 ~10 3 Mega 2 20 ~10 6 Giga 2 30 ~10 9 Tera 2 40 ~10 12 Peta 2 50 ~10 15 Exa 2 60 ~10 18

Byte ordering Big endian: store higher place values at lower addresses Most-significant byte (MSB) to least-significant byte (LSB) Similar to standard way to write hex (implied with 0x prefix) Little endian: store lower place values at lower addresses Least-significant byte (LSB) to most-significant byte (MSB) Default byte ordering on most Intel-based machines low addr high addr 0x11223344 in big endian: 11 22 33 44 0x11223344 in little endian: 44 33 22 11

Byte ordering examples Big endian: most significant byte first (MSB to LSB) Little endian: least significant byte first (LSB to MSB) low high 0x11223344 in big endian: 11 22 33 44 0x11223344 in little endian: 44 33 22 11 Decimal: 1 16-bit big endian: 00000000 00000001 (hex: 00 01) 16-bit little endian: 00000001 00000000 (hex: 01 00) Decimal: 19 (16+2+1) 16-bit big endian: 00000000 00010011 (hex: 00 13) 16-bit little endian: 00010011 00000000 (hex: 13 00) Decimal: 256 16-bit big endian: 00000001 00000000 (hex: 01 00) 16-bit little endian: 00000000 00000001 (hex: 00 01)

Character encodings ASCII ("American Standard Code for Information Interchange") 1-byte code developed in 1960s Limited support for non-english characters Unicode Multi-byte code developed in 1990s "All the characters for all the writing systems of the world" Over 136,000 characters in latest standard Fixed-width (UTF-16 and UTF-32) and variable-width (UTF-8) UTF-8

Program encodings Machine code Binary encoding of opcodes and operands Specific to a particular CPU architecture (e.g., x86_64) int add (int num1, int num2) { return num1 + num2; } 0000000000400606 <add>: 400606: 55 push %rbp 400607: 48 89 e5 mov %rsp,%rbp 40060a: 89 7d fc mov %edi,-0x4(%rbp) 40060d: 89 75 f8 mov %esi,-0x8(%rbp) 400610: 8b 55 fc mov -0x4(%rbp),%edx 400613: 8b 45 f8 mov -0x8(%rbp),%eax 400616: 01 d0 add %edx,%eax 400618: 5d pop %rbp 400619: c3 retq

Bitwise operations Basic bitwise operations & (and) (or) ^ (xor) Not boolean algebra! && (and) (or)! (not) 0 (false) non-zero (true) Important properties: x & 0 = 0 x & 1 = x x 0 = x x 1 = 1 x ^ 0 = x x ^ x = ~x x ^ x = 0 & 0 1 0 0 0 1 0 1 Commutative: x & y = y & x x y = y x x ^ y = y ^ x Associative: (x & y) & z = x & (y & z) (x y) z = x (y z) (x ^ y) ^ z = x ^ (y ^ z) Distributive: x & (y z) = (x & y) (x & z) x (y & z) = (x y) & (x z) 0 1 0 0 1 1 1 1 ^ 0 1 0 0 1 1 1 0 AND OR XOR

Bitwise operations Bitwise complement (~) - flip the bits ~0000 = 1111 (~0 = 1) ~1010 = 0101 (~0xA = 0x5) Also called ones' complement (useful in next class) Left shift (<<) and right shift (>>) Equivalent to multiplying (<<) or dividing (>>) by two Left shift: 0110 << 1 = 1100 1 << 3 = 8 Logical right shift (fill zeroes): 1100 >> 2 = 0011 Arithmetic right shift (fill most sig. bit): 1100 >> 2 = 1111 (but only if unsigned) 0100 >> 2 = 0001 On stu: int: 0f000000 >> 8 = 000f0000 int: ff000000 >> 8 = ffff0000 uint: 0f000000 >> 8 = 000f0000 uint: ff000000 >> 8 = 00ff0000 (arithmetic) (logical)

Masking Bitwise operations can extract parts of a binary value This is referred to as masking; specify a bit pattern mask to indicate which bits you want Helpful fact: 0xF is all 1 s in binary! Use a bitwise AND (&) with the mask to extract the bits Use a bitwise complement (~) to invert a mask Example: To extract the lower-order 16 bits of a larger value v, use v & 0xFFFF 0xDEADBEEF & 0xFFFF = 0x0000BEEF = 0xBEEF 0xDEADBEEF & 0x0000FFFF = 0x0000BEEF = 0xBEEF 0xDEADBEEF & 0xFFFF0000 = 0xDEAD0000 0xDEADBEEF & ~0xFFFF = 0xDEAD0000 0xDEADBEEF & ~0x0000FFFF = 0xDEAD0000