CS 241 Data Organization Binary

Similar documents
Beginning C Programming for Engineers

CSCI 2212: Intermediate Programming / C Chapter 15

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

2.1. Unit 2. Integer Operations (Arithmetic, Overflow, Bitwise Logic, Shifting)

Computer Organization & Systems Exam I Example Questions

C expressions. (Reek, Ch. 5) 1 CS 3090: Safety Critical Programming in C

CS 107 Lecture 2: Bits and Bytes (continued)

More about Binary 9/6/2016

Midterm Exam Review Slides

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

CS 24: INTRODUCTION TO. Spring 2015 Lecture 2 COMPUTING SYSTEMS

CS 253. January 14, 2017

AGENDA Binary Operations CS 3330 Samira Khan

Number Representations

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

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

Variables and literals

CS 31: Intro to Systems Binary Arithmetic. Martin Gagné Swarthmore College January 24, 2016

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

Survey. Motivation 29.5 / 40 class is required

CS 31: Introduction to Computer Systems. 03: Binary Arithmetic January 29

Representation of Information

Expression and Operator

Lecture 13 Bit Operations

Programming. Elementary Concepts

Under the Hood: Data Representations, Memory and Bit Operations. Computer Science 104 Lecture 3

Solutions - Homework 2 (Due date: October 4 5:30 pm) Presentation and clarity are very important! Show your procedure!

Signed Binary Numbers

Review Topics. Midterm Exam Review Slides

Programming Project 2

int main() { return 0; }

Applied Computer Programming

ME 461 C review Session Fall 2009 S. Keres

More on C programming

C-string format with scanf/printf

Binary representation of integer numbers Operations on bits

UNIVERSITY OF LIMERICK OLLSCOIL LUIMNIGH COLLEGE OF INFORMATICS & ELECTRONICS DEPARTMENT OF ELECTRONIC & COMPUTER ENGINEERING

Arithmetic and Bitwise Operations on Binary Data

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Arithmetic and Bitwise Operations on Binary Data

Binary Representations and Arithmetic

A flow chart is a graphical or symbolic representation of a process.

C-string format with scanf/printf

Unsigned Binary Integers

Unsigned Binary Integers

Chapter 12 Variables and Operators

Lecture 17 Bit Operations

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

Content. Information Coding. Where are we? Where are we? Bit vectors (aka Words )

LAB A Translating Data to Binary

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

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

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

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

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

Topic Notes: Bits and Bytes and Numbers

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

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Number Systems for Computers. Outline of Introduction. Binary, Octal and Hexadecimal numbers. Issues for Binary Representation of Numbers

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

Integer Representation

CS 31: Intro to Systems Binary Arithmetic. Kevin Webb Swarthmore College January 26, 2016

Introduction to Computing Systems Fall Lab # 3

Topic Notes: Bits and Bytes and Numbers

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

Arithmetic Operations

Basic Arithmetic (adding and subtracting)

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

A complement number system is used to represent positive and negative integers. A complement number system is based on a fixed length representation

Bits, Bytes and Integers

Chapter 2. Instruction Set. RISC vs. CISC Instruction set. The University of Adelaide, School of Computer Science 18 September 2017

EECE.2160: ECE Application Programming

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

CS61, Fall 2012 Midterm Review Section

Memory, Data, & Addressing II CSE 351 Spring

CSCI 2021: Binary, Integers, Arithmetic

Where we are going (today)

Number System. Introduction. Decimal Numbers

Computer Architecture

JAVA OPERATORS GENERAL

Do not start the test until instructed to do so!

Integers II. CSE 351 Autumn Instructor: Justin Hsia

CS360 Midterm 1 - February 21, James S. Plank. Put all answers on the answer sheet. In all of these questions, please assume the following:

Recap from Last Time. CSE 2021: Computer Organization. It s All about Numbers! 5/12/2011. Text Pictures Video clips Audio

The Design of C: A Rational Reconstruction

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Data Representation 1

Fundamentals of Programming

The Design of C: A Rational Reconstruction"

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

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

CS356: Discussion #3 Floating-Point Operations. Marco Paolieri

The Arithmetic Operators

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

CS16 Exam #1 7/17/ Minutes 100 Points total

CS 33. Data Representation, Part 1. CS33 Intro to Computer Systems VII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

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

Transcription:

CS 241 Data Organization Binary Brooke Chenoweth University of New Mexico Fall 2017

Combinations and Permutations In English we use the word combination loosely, without thinking if the order of things is important. In other words: My fruit salad is a combination of apples, grapes and bananas. In this statement, order does not matter: bananas, grapes and apples or grapes, apples and bananas make the same salad. The combination to the safe is 472. Here the order is important: 724 would not work, nor would 247.

Combinations and Permutations In Computer Science and Math, we use more precise language: If the order doesn t matter, it is a Combination. If the order does matter it is a Permutation. Repetition is Allowed: such as the lock above. It could be 333. No Repetition: for example the first three people in a running race. Order does matter, but you can t be first and second.

Information in a Binary Signal 1 Bit 2 Permutations 0 1 2 Bits 4 Permutations 0 0 0 1 1 0 1 1 3 Bits 8 Permutations 000 0 001 1 010 2 011 3 100 4 101 5 110 6 111 7 4 Bits 16 Permutations 0000 1000 0001 1001 0010 1010 0011 1011 0100 1100 0101 1101 0110 1110 0111 1111

Numbers in Base Ten and Base Two Base 10 5307 = 5 10 3 + 3 10 2 + 0 10 1 + 7 10 0 = 5000 + 300 + 0 + 7 Base 2 1011 = 1 2 3 + 0 2 2 + 1 2 1 + 1 2 0 = 8 + 0 + 2 + 1

Examples of Binary Numbers 0 0 0 0 1 0 0 0 1 1 = 35 512 256 128 64 32 16 8 4 2 1 0 0 0 0 1 1 1 1 1 1 = 63 512 256 128 64 32 16 8 4 2 1 0 0 0 1 0 0 0 0 0 0 = 64 512 256 128 64 32 16 8 4 2 1 1 1 0 1 1 0 0 0 1 1 = 867 512 256 128 64 32 16 8 4 2 1

Hexadecimal: Base-16 Hexadecimal (or hex) is a base-16 system that uses sixteen distinct symbols, most often the symbols 09 to represent values zero to nine, and A, B, C, D, E, F to represent values ten to fifteen. Base 16 0x53AC = 5 16 3 +3 16 2 +10 16 1 +12 16 0 = 5 4096 +3 256 +10 16 +12 1 = 20,480 +768 +160 +12 = 21,420

Why Hexadecimal? Hexadecimal is more compact than base-10 Hexadecimal is way more compact that base-2 Since 16 is a power of 2, it is very easy to convert between Binary and Hexadecimal Base 16 Four bytes: 0x01239ACF 01 23 9A BF 0000 0001 0010 0011 1001 1010 1011 1111

Hexadecimal Literals # include <stdio.h> void main ( void ) { printf ("%d\n", 0x1 ); printf ("%d\n", 0x2 ); printf ("%d\n", 0x3 ); printf ("%d\n", 0x8 ); printf ("%d\n", 0x9 ); printf ("%d\n", 0xA ); printf ("%d\n", 0xB ); printf ("%d\n", 0xC ); printf ("%d\n", 0xD ); printf ("%d\n", 0xE ); printf ("%d\n", 0xF ); printf ("%d\n", 0 x10 ); printf ("%d\n", 0 x11 ); printf ("%d\n", 0 x12 ); 1 2 3 8 9 10 11 12 13 14 15 16 17 18

Hexadecimal Literals (using %x) # include <stdio.h> void main ( void ) { printf ("%x\n", 0x1 ); printf ("%x\n", 0x2 ); printf ("%x\n", 0x3 ); printf ("%x\n", 0x8 ); printf ("%x\n", 0x9 ); printf ("%x\n", 0xA ); printf ("%x\n", 0xB ); printf ("%x\n", 0xC ); printf ("%x\n", 0xD ); printf ("%x\n", 0xE ); printf ("%x\n", 0xF ); printf ("%x\n", 0 x10 ); printf ("%x\n", 0 x11 ); printf ("%x\n", 0 x12 ); 1 2 3 8 9 a b c d e f 10 11 12

Powers of 2: char, int # include <stdio.h> void main ( void ) { char i =0; char a =1; unsigned char b =1; int c = 1; for (i =1; i <22; i ++) { a = a * 2; b = b * 2; c = c * 2; printf ("%2d) %4d %3d %7d\n", i, a, b, c); 1) 2 2 2 2) 4 4 4 3) 8 8 8 4) 16 16 16 5) 32 32 32 6) 64 64 64 7) -128 128 128 8) 0 0 256 9) 0 0 512 10) 0 0 1024 11) 0 0 2048 12) 0 0 4096 13) 0 0 8192 14) 0 0 16384 15) 0 0 32768 16) 0 0 65536 17) 0 0 131072 18) 0 0 262144 19) 0 0 524288 20) 0 0 1048576

# include <stdio.h> void main ( void ) { char i =0; int c =1; long d = 1; for (i =1; i <65; i ++) { c = c * 2; d = d * 2; Powers of 2: int, long printf ("%2d) %11 d %20 ld\n", i, c, d); Format code: ld for long decimal... 29) 536870912 536870912 30) 1073741824 1073741824 31) -2147483648 2147483648 32) 0 4294967296 33) 0 8589934592... 61) 0 2305843009213693952 62) 0 4611686018427387904 63) 0-9223372036854775808 64) 0 0

Bit Operations C provides several operators for manipulating the individual bits of a value: & bitwise AND 1010 & 0011 = 0010 bitwise OR 1010 0011 = 1011 ^ bitwise XOR 1010 ^ 0011 = 1001 ~ bitwise NOT ~1010 = 0101 << left-shift 00000100 << 3 = 00100000 >> right-shift 00000100 >> 2 = 00000001

Shift Operator Example void main ( void ) { int i; for (i =0; i <8; i ++) { unsigned char n = 1 << i; printf ("n=%d\n", n); Output: n=1 n=2 n=4 n=8 n=16 n=32 n=64 n=128

Convert 77 to an 8-bit Binary String 2 7 = 128 is > 77, put a 0 in the 128s place 0 2 6 = 64 is <= 77, put a 1 in the 64s place 01 and subtract 64: 77-64 = 13 2 5 = 32 is > 13, put a 0 in the 32s place 010 2 4 = 16 is > 13, put a 0 in the 16s place 0100 2 3 = 8 is <= 13, put a 1 in the 8s place 01001 and subtract 8: 13-8 = 5 2 2 = 4 is <= 5, put a 1 in the 4s place 010011 and subtract 4: 5-4 = 1 2 1 = 2 is > 1, put a 0 in the 2s place 0100110 2 0 = 1 is <= 1, put a 1 in the 1s place 01001101 and subtract 1: 1-1 = 0

Convert unsigned char to Binary Array # include <stdio.h> void main ( void ) { char bits [9]; bits [8] = \0 ; unsigned char n =83; unsigned char powerof2 = 128; int i; for (i =0; i <=7; i ++) { if ( n >= powerof2 ) { bits [i] = 1 ; n = n- powerof2 ; else bits [i] = 0 ; powerof2 /= 2; printf ("%s\n", bits ); Output: 01010011 This allows us to print out the binary value in human readable form. Can be useful when debugging. (or use hexadecimal!)

Masking void main ( void ) { long mask = 1 < <23; /* 1 in position of interest */ long x = 25214903917; /* Not zero if bit 23 is ON in x. */ printf ("%ld\n", x & mask ); /* prints : 8388608 */ /* Turn ON bit - 23. If already ON, x is unchanged. */ x = x mask ; printf ("%ld\n", x); /* prints : 25214903917 */ /* Turn OFF bit 23. If already OFF, x is unchanged. x = x & (~ mask ); printf ("%ld\n", x); /* prints : 25206515309 */

Using a Mask: Binary Array # include <stdio.h> void main ( void ) { char bits [9]; bits [8] = \0 ; unsigned char n =83; unsigned char powerof2 = 128; int i; for (i =0; i <=7; i ++) { if(n & powerof2 ) { bits [i] = 1 ; else bits [i] = 0 ; powerof2 = powerof2 >> 1; printf ("%s\n", bits ); Output: 01010011 In the earlier slide, whenever a power of 2 is found, it is subtracted from n. This method never changes n.

Bit-operations: x = (1<<n) Set bit n in variable x. (1<<n) shifts 1 left by n bits. The result is OR ed into x. 0 0 0 0 0 1 << 2 X 0 0 0 1 0 0 1 0 0 0 0 1 1 0 0 1 0 1

Bit-operations: x & (1<<n) Test bit n in variable x. (1<<n) creates a value with the appropriate bit set by shifting 1 left by n bits. It is AND ed with x to see if that bit is set in x. 0 0 0 0 0 1 << 2 X 0 0 0 1 0 0 & 1 0 0 1 0 1 0 0 0 1 0 0

Bit-operations: x & ~(1<<n) Clear bit n in variable x. (1<<n) creates a value with the appropriate bit set by shifting 1 left by n bits. The one s complement operation ~ flips all the bits in the value, resulting in a value with every bit but the n th set. It is AND ed with x to clear the n th bit but leave the rest unchanged.

Bit-operations: x & ~(1<<n) 0 0 0 0 0 1 << 2 0 0 0 1 0 0 ~ 1 1 1 0 1 1 X & 1 0 0 1 0 1 1 0 0 0 0 1

Bit-operations: x = (1<<n)-1 Set n lowest bits in variable x. We create a mask with all ones in the lower n bits by shifting 1 left by n bits and subtracting 1. It is OR ed with x to set the n lowest bits but leave the rest unchanged.

Bit-operations: x = (1<<n)-1 0 0 0 0 0 1 << 3 0 0 1 0 0 0 1 0 0 0 1 1 1 X 0 1 0 0 0 1 0 1 0 1 1 1

Bit-operations: (x>>p)&((1<<n)-1) Suppose we want to extract n bits from x, starting at position p. We create a mask with all ones in the lower n bits the same way as before: shift 1 left by n bits and subtract 1. Next, we shift x right by p bits. Finally, we AND the mask and (x>>p) to strip out any extra high-order bits.

Bit-operations: (x>>2)&((1<<3)-1) 0 0 0 0 0 1 << 3 1 X 1 0 1 0 1 >> 2 0 0 1 0 0 0 1 0 0 1 1 0 1 & 0 0 0 1 1 1 0 0 0 1 0 1

Addition: Base 10 and Binary Base 10 Binary 1 1 1 1 2 9 0 0 0 1 1 1 0 1 + 5 6 + 0 0 1 1 1 0 0 0 8 5 0 1 0 1 0 1 0 1

Overflow Addition Output: # include <stdio.h> void main ( void ) 124 124-3 253 { 125 125-2 254 char i =0; 126 126-1 255 char a = 123, b = 252; 127 127 0 0 unsigned char x = 123, y = 252; -128 128 1 1 for (i =1; i <=7; i ++) -127 129 2 2 { a ++; b ++; x ++; y ++; -126 130 3 3 printf ("%4d %4d %4d %4d\n", a, x, b, y);

From ordinary binary: Flip the bits and Add 1. Ordinary Binary Decimal 0000 0001 1 0000 0010 2 0000 0011 3 0000 0100 4 0000 0101 5 0000 0010 6 0000 0111 7 Two s Complement 5 0 0 0 0 1 0 1 Flip Bits 1 1 1 1 0 1 0 Add 1 0 0 0 0 0 0 1-5 1 1 1 1 0 1 1 Two s Complement Decimal 1111 1111-1 1111 1110-2 1111 1101-3 1111 1100-4 1111 1011-5 1111 1010-6 1111 1001-7

Two s Complement Addition 1 1 1 1 1 1 1 1 2 9 0 0 0 1 1 1 0 1 + -2 9 + 1 1 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 7 0 0 0 0 0 1 1 1 + -4 + 1 1 1 1 1 1 0 0 3 0 0 0 0 0 0 1 1