Fundamentals of Programming

Similar documents
Fundamentals of Programming (C)

ITEC 1011 Introduction to Information Technologies

Common Number Systems

College of Computer and Information Sciences Department of Computer Science. CSC 220: Computer Organization. Unit1 Number Systems

Data Representation and Binary Arithmetic. Lecture 2

Bits and Bytes. Data Representation. A binary digit or bit has a value of either 0 or 1; these are the values we can store in hardware devices.

1.1. INTRODUCTION 1.2. NUMBER SYSTEMS

LOGIC DESIGN. Dr. Mahmoud Abo_elfetouh

Number System (Different Ways To Say How Many) Fall 2016

Chapter 2 Bits, Data Types, and Operations

Chapter 2 Number System

Chapter 2 Bits, Data Types, and Operations

CPS 104 Computer Organization and Programming Lecture-2 : Data representations,

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

Chapter 2 Bits, Data Types, and Operations

DATA REPRESENTATION. Data Types. Complements. Fixed Point Representations. Floating Point Representations. Other Binary Codes. Error Detection Codes

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

3.1. Unit 3. Binary Representation

Number Systems Base r

plc numbers Encoded values; BCD and ASCII Error detection; parity, gray code and checksums

Chapter 2 Bits, Data Types, and Operations

Positional Number System

Unit 3. Analog vs. Digital. Analog vs. Digital ANALOG VS. DIGITAL. Binary Representation

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

ASSIGNMENT 5 TIPS AND TRICKS

EE 109 Unit 3. Analog vs. Digital. Analog vs. Digital. Binary Representation Systems ANALOG VS. DIGITAL

Number Representations

Numbers and Computers. Debdeep Mukhopadhyay Assistant Professor Dept of Computer Sc and Engg IIT Madras

Chapter 2 Bits, Data Types, and Operations

Oberon Data Types. Matteo Corti. December 5, 2001

CMSC 313 Lecture 03 Multiple-byte data big-endian vs little-endian sign extension Multiplication and division Floating point formats Character Codes

Chapter 3. Information Representation

EE 109 Unit 2. Binary Representation Systems

Binary Numbers. The Basics. Base 10 Number. What is a Number? = Binary Number Example. Binary Number Example

EE 109 Unit 2. Analog vs. Digital. Analog vs. Digital. Binary Representation Systems ANALOG VS. DIGITAL

The Binary Number System

CSE-1520R Test #1. The exam is closed book, closed notes, and no aids such as calculators, cellphones, etc.

CSE-1520R Test #1. The exam is closed book, closed notes, and no aids such as calculators, cellphones, etc.

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, FALL 2012

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

Lecture (09) x86 programming 8

CS341 *** TURN OFF ALL CELLPHONES *** Practice NAME

Unit 3, Lesson 2 Data Types, Arithmetic,Variables, Input, Constants, & Library Functions. Mr. Dave Clausen La Cañada High School

Chapter 8. Characters and Strings

UNIT 2 NUMBER SYSTEM AND PROGRAMMING LANGUAGES

Experiment 3. TITLE Optional: Write here the Title of your program.model SMALL This directive defines the memory model used in the program.

2a. Codes and number systems (continued) How to get the binary representation of an integer: special case of application of the inverse Horner scheme

Do not start the test until instructed to do so!

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, SPRING 2013

Introduction to Decision Structures. Boolean & If Statements. Different Types of Decisions. Boolean Logic. Relational Operators

Fundamental Data Types

5/17/2009. Digitizing Discrete Information. Ordering Symbols. Analog vs. Digital

Under the Hood: Data Representation. Computer Science 104 Lecture 2

Do not start the test until instructed to do so!

Number System. Introduction. Decimal Numbers

Variables and data types

Exercises Software Development I. 03 Data Representation. Data types, range of values, internal format, literals. October 22nd, 2014

CSC 8400: Computer Systems. Represen3ng and Manipula3ng Informa3on. Background: Number Systems

DIGITAL SYSTEM DESIGN

CPSC 301: Computing in the Life Sciences Lecture Notes 16: Data Representation

Chapter 1. Hardware. Introduction to Computers and Programming. Chapter 1.2

Hardware. ( Not so hard really )

EXPERIMENT 8: Introduction to Universal Serial Asynchronous Receive Transmit (USART)

EXPERIMENT 7: Introduction to Universal Serial Asynchronous Receive Transmit (USART)

PureScan - ML1. Configuration Guide. Wireless Linear Imager Wireless Laser scanner - 1 -

Do not start the test until instructed to do so!

Source coding and compression

Connecting UniOP to Datalogic Barcode Readers

FA269 - DIGITAL MEDIA AND CULTURE

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, FALL 2012

Imperial College London Department of Computing

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

User s Manual. Xi3000 Scanner. Table of Contents

void mouseclicked() { // Called when the mouse is pressed and released // at the same mouse position }

Number Systems II MA1S1. Tristan McLoughlin. November 30, 2013

4/14/2015. Architecture of the World Wide Web. During this session we will discuss: Structure of the World Wide Web

S-Series Sensor ASCII Protocol v8.1.0

EE292: Fundamentals of ECE

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

Simple Data Types in C. Alan L. Cox

n NOPn Unary no operation trap U aaa NOP Nonunary no operation trap i

Xi2000-BT Series Configuration Guide

Operations On Data CHAPTER 4. (Solutions to Odd-Numbered Problems) Review Questions

Table of Contents Sleep Settings How to Configure the Scanner. 7 Chapter 2 System Setup

Introduction. Chapter 1. Hardware. Introduction. Creators of Software. Hardware. Introduction to Computers and Programming (Fall 2015, CSUS)

1. Character/String Data, Expressions & Intrinsic Functions. Numeric Representation of Non-numeric Values. (CHARACTER Data Type), Part 1

Configuration Manual PULSAR C CCD SCANNER. Table of Contents

You are unfamiliar with the principles of modern computer architecture and you would like to come to terms with the basic concepts

Coding Theory. Networks and Embedded Software. Digital Circuits. by Wolfgang Neff

Chemistry Hour Exam 2

RS-232 Digital Relay I/O

CSE 30 Fall 2013 Final Exam

FD-011WU. 2D Barcode Reader User Guide V1.6CC

Universal Asynchronous Receiver Transmitter Communication

ASCII Code - The extended ASCII table

Digital Systems and Binary Numbers

Review. Single Pixel Filters. Spatial Filters. Image Processing Applications. Thresholding Posterize Histogram Equalization Negative Sepia Grayscale

SE311: Design of Digital Systems

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

Transcription:

Fundamentals of Programming Lecture 2 Number Systems & Arithmetic Lecturer : Ebrahim Jahandar Some Parts borrowed from slides by IETC1011-Yourk University

Common Number Systems System Base Symbols Used by humans? Used in computers? Decimal 10 0, 1, 9 Yes No Binary 2 0, 1 No Yes Octal 8 0, 1, 7 No No Hexa-decimal 16 0, 1, 9, A, B, F Yes No 2

Quantities/Counting (1 of 3) Decimal Binary Octal Hexa-decimal 0 0 0 0 1 1 1 1 2 10 2 2 3 11 3 3 4 100 4 4 5 101 5 5 6 110 6 6 7 111 7 7 3

Quantities/Counting (2 of 3) Decimal Binary Octal Hexa-decimal 8 1000 10 8 9 1001 11 9 10 1010 12 A 11 1011 13 B 12 1100 14 C 13 1101 15 D 14 1110 16 E 15 1111 17 F 4

Quantities/Counting (3 of 3) Decimal Binary Octal Hexa-decimal 16 10000 20 10 17 10001 21 11 18 10010 22 12 19 10011 23 13 20 10100 24 14 21 10101 25 15 22 10110 26 16 23 10111 27 17 5

The possibilities: Conversion Among Bases Decimal Octal Binary Hexadecimal pp. 40-46 6

Quick Example 25 10 = 11001 2 = 31 8 = 19 16 Base 7

Decimal to Decimal (just for fun) Decimal Octal Binary Hexadecimal Next slide 8

Weight 125 10 => 5 x 10 0 = 5 2 x 10 1 = 20 1 x 10 2 = 100 125 Base 9

Binary to Decimal Decimal Octal Binary Hexadecimal 10

Technique Binary to Decimal Multiply each bit by 2 n, where n is the weight of the bit The weight is the position of the bit, starting from 0 on the right Add the results 11

Bit 0 Example 101011 2 => 1 x 2 0 = 1 1 x 2 1 = 2 0 x 2 2 = 0 1 x 2 3 = 8 0 x 2 4 = 0 1 x 2 5 = 32 43 10 12

Hexadecimal to Decimal Decimal Octal Binary Hexadecimal 13

Technique Hexadecimal to Decimal Multiply each bit by 16 n, where n is the weight of the bit The weight is the position of the bit, starting from 0 on the right Add the results 14

Example ABC 16 => C x 16 0 = 12 x 1 = 12 B x 16 1 = 11 x 16 = 176 A x 16 2 = 10 x 256 = 2560 2748 10 15

Decimal to Binary Decimal Octal Binary Hexadecimal 16

Technique Decimal to Binary Divide by two, keep track of the remainder First remainder is bit 0 (LSB, least-significant bit) Second remainder is bit 1 Etc. 17

Example 125 10 =? 2 2 125 2 62 1 2 31 0 2 15 1 2 7 1 2 3 1 2 1 1 0 1 125 10 = 1111101 2 18

Hexadecimal to Binary Decimal Octal Binary Hexadecimal 19

Technique Hexadecimal to Binary Convert each hexadecimal digit to a 4-bit equivalent binary representation 20

10AF 16 =? 2 Example 1 0 A F 0001 0000 1010 1111 10AF 16 = 0001000010101111 2 21

Decimal to Octal Decimal Octal Binary Hexadecimal 22

Technique Divide by 8 Keep track of the remainder Decimal to Octal 23

1234 10 =? 8 Example 8 1234 8 154 2 8 19 2 8 2 3 0 2 1234 10 = 2322 8 24

Decimal to Hexadecimal Decimal Octal Binary Hexadecimal 25

Technique Decimal to Hexadecimal Divide by 16 Keep track of the remainder 26

1234 10 =? 16 Example 16 1234 16 77 2 16 4 13 = D 0 4 1234 10 = 4D2 16 27

Binary to Octal Decimal Octal Binary Hexadecimal 28

Technique Binary to Octal Group bits in threes, starting on right Convert to octal digits 29

1011010111 2 =? 8 Example 1 011 010 111 1 3 2 7 1011010111 2 = 1327 8 30

Binary to Hexadecimal Decimal Octal Binary Hexadecimal 31

Technique Binary to Hexadecimal Group bits in fours, starting on right Convert to hexadecimal digits 32

1010111011 2 =? 16 Example 10 1011 1011 2 B B 1010111011 2 = 2BB 16 33

Exercise Convert... Decimal Binary Octal Hexa-decimal 33 1110101 703 1AF 34

Exercise Convert Decimal Binary Octal Hexa-decimal 33 100001 41 21 117 1110101 165 75 451 111000011 703 1C3 431 110101111 657 1AF 35

Common Powers (1 of 2) Base 10 Power Preface Symbol 10-12 pico p 10-9 nano n 10-6 micro 10-3 milli m 10 3 kilo k 10 6 mega M 10 9 giga G 10 12 tera T Value.000000000001.000000001.000001.001 1000 1000000 1000000000 1000000000000 36

Common Powers (2 of 2) Base 2 Power Preface Symbol 2 10 kilo k 2 20 mega M 2 30 Giga G Value 1024 1048576 1073741824 What is the value of k, M, and G? In computing, particularly w.r.t. memory, the base-2 interpretation generally applies 37

Review multiplying powers For common bases, add powers a b a c = a b+c 2 6 2 10 = 2 16 = 65,536 or 2 6 2 10 = 64 2 10 = 64k 38

Two 1-bit values Binary Addition (1 of 2) A B A + B 0 0 0 0 1 1 1 0 1 1 1 10 two 39

Two n-bit values Binary Addition (2 of 2) Add individual bits Propagate carries E.g., 1 1 10101 21 + 11001 + 25 101110 46 40

Decimal (just for fun) Multiplication (1 of 3) 35 x 105 175 000 35 3675 41

Multiplication (2 of 3) Binary, two 1-bit values A B A B 0 0 0 0 1 0 1 0 0 1 1 1 42

Multiplication (3 of 3) Binary, two n-bit values As with decimal values E.g., 1110 x 1011 1110 1110 0000 1110 10011010 43

Signed Integer Representation Negative numbers must be encode in binary number systems Well-known methods Sign-magnitude One s Complement Two s Complement Which one is better? Calculation speed Complexity of the computation circuit 44

Sign-magnitude One sign bit + magnitude bits Positive: s = 0 Negative: s= 1 Range = {(-127) 10.. (+127) 10 } Two ways to represent zero: 00000000 (+0) 10000000 ( 0) Examples: (+ 43) 10 = 00101011 (- 43) 10 = 10101011 7 s Sign 6 5 4 3 2 Magnitude 1 0 How many positive and negative integers can be represented using N bits? Positive: 2 N-1-1 Negative: 2 N-1-1 45

Two s Complement Negative numbers: 1. Invert all the bits through the number ~(0) = 1 ~(1) = 0 2. Add one Example: +1 = 00000001-1 =? ~(00000001) 11111110 11111110 + 1 11111111 Only one zero (00000000) Range = {127.. 128} 46

A proposition is a statement that can be either true or false The sky is blue I is a English major x == y Not propositions: Are you Bob? x = 7 Boolean propositions 47

We use Boolean variables to refer to propositions Usually are lower case letters starting with p (i.e. p, q, r, s, etc.) A Boolean variable can have one of two values true (T) or false (F) A proposition can be Boolean variables A single variable: p An operation of multiple variables: p (q r) 48

Introduction to Logical Operators About a dozen logical operators Similar to algebraic operators + * - / In the following examples, p = Today is Friday q = Today is my birthday 49

Logical operators: Not A not operation switches (negates) the truth value Symbol: or ~ In C++ and Java, the operand is! p = Today is not Friday p T F p F T 50

Logical operators: And An and operation is true if both operands are true Symbol: It s like the A in And In C++ and Java, the operand is && p q = Today is Friday and today is my birthday p q p q T T T T F F F T F F F F 51

Logical operators: Or An or operation is true if either operands are true Symbol: In C++ and Java, the operand is p q = Today is Friday or today is my birthday (or possibly both) p q p q T T T T F T F T T F F F 52

Logical operators: Exclusive Or An exclusive or operation is true if one of the operands are true, but false if both are true Symbol: Often called XOR p q (p q) (p q) In Java, the operand is ^ (but not in C++) p q = Today is Friday or today is my birthday, but not both p q p q T T F T F T F T T F F F 53

ASCII Codes American Standard Code for Information Interchange First decision: 7 bits for representation Final decision: 8 bits for representation 256 characters ASCII ( P ) = (50) 16 ASCII ( = ) = (3D) 16 row number column number Hexadecimal 0 1 2 3 4 5 6 7 0 NUL DLE 0 @ P ` p 1 SOH DC1! 1 A Q a q 2 STX DC2 " 2 B R b r 3 ETX DC3 # 3 C S c s 4 EOT DC4 $ 4 D T d t 5 ENQ NAK % 5 E U e u 6 ACK SYN & 6 F V f v 7 BEL ETB ' 7 G W g w 8 BS CAN ) 8 H X h x 9 TAB EM ( 9 I Y i y A LF SUB * : J Z j z B VT ESC + ; K ] k } C FF FS, > L \ l D CR GS - = M [ m { E SO RS. < N ^ n ~ F SI US /? O _ o 54

Summary Numeral Systems Decimal, Binary, Octal, Hexadecimal Computer Data Storage Units Bit, Byte, Kilo byte, Giga byte, Numeral Systems Conversion Convert between different bases Calculations in Number Systems Addition and multiplication Boolean Algebra Boolean Operations Signed Integer Representation Sing-magnitude: one sign bit + magnitude bits Two s complement : (-N) = ~(N) + 1 ASCII Codes 55