Tutorial 7.

Slides:



Advertisements
Similar presentations
Booth's Algorithm Example
Advertisements

Binary Addition Rules Adding Binary Numbers = = 1
Binary & Decimal numbers = 3* * *10 + 5*1 = 3* * * *10 0 Decimal system: Ten digits: 0,1,2,3,…,9 Example:
Representing Information as Bit Patterns Lecture 4 CSCI 1405, CSCI 1301 Introduction to Computer Science Fall 2009.
Mathematics with Binary. Question  Below is a binary string  Which is the least significant bit (LSB)?  Which is the most significant bit (MSB)? 0.
DIGITAL SYSTEMS TCE1111 Representation and Arithmetic Operations with Signed Numbers Week 6 and 7 (Lecture 1 of 2)
1 Lecture 4: Arithmetic for Computers (Part 4) CS 447 Jason Bakos.
1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.
1 Bitwise Operators. 2 Bitwise Operators (integers) Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise "ones complement"
They are the same as registers since they store binary numbers. Called shifting registers since they shift (left or right) the binary number stored in.
Converting binary to decimal decimal to binary
Convert Decimal to Floating point number [IEEE 754]
Revision Introductory Lesson
Binary and Hexadecimal Numbers
Computer Systems 1 Fundamentals of Computing Negative Binary.
Number Systems.
1 Arithmetic and Logical Operations - Part II. Unsigned Numbers Addition in unsigned numbers is the same regardless of the base. Given a pair of bit sequences.
Chapter 7 Arithmetic Operations and Circuits Binary Arithmetic Addition –When the sum exceeds 1, carry a 1 over to the next-more-significant column.
Lecture 7 How computers process data (Number Systems) PRESENTED BY MD. MAHBUBUL ALAM, PHD 1.
A little bit of exercise.. ;). Exercise Given to you are some binary to decimal examples : Given to you are some binary to decimal examples : Tens Units.
Multiplying and Dividing Decimals by 10, 100, and 1,000
1 CS103 Guest Lecture Number Systems & Conversion Bitwise Logic Operations.
Chapter1: Number Systems
Number Systems Prepared by Department of Preparatory year.
Number Systems Binary to Decimal Octal to Decimal Hexadecimal to Decimal Binary to Octal Binary to Hexadecimal Two’s Complement.
Lesson 4-7 Example Example 1 Find 4.32 × Multiply the factors, ignoring the decimal points for now. 432 × 6 = 2592.
Two’s Complement. A system used to represent a negative number in binary A system used to represent a negative number in binary Positive numbers start.
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=
Boolean Algebra Binary Arithmetic August 28, 2001 Ellen Spertus MCS 111.
Introduction To Number Systems Binary System M. AL-Towaileb1.
Lecture 6 Excess Notation. Excess 8 notation indicates that the value for zero is the bit pattern for 8, that is 1000 Excess 8 notation indicates that.
Computer Number System
ECE 3110: Introduction to Digital Systems Number Systems.
CSED101 INTRODUCTION TO COMPUTING SUM TYPE 유환조 Hwanjo Yu.
Let’s remember 2 things. 1. Multiplying by 10,100,1000 Multiply by 10 Multiply by 100 Multiply by 1000 Add one 0 Add two 0’sAdd three 0’s since 10 has.
In decimal we are quite familiar with placing a “-” sign in front of a number to denote that it is negative The same is true for binary numbers a computer.
Integer Operations Computer Organization and Assembly Language: Module 5.
Binary Addition and Subtraction. Arithmetic Operations Arithmetic operations in a computer are done using binary numbers and not decimal numbers and these.
Number Representation 1 Lecture 2. Outcomes By the end of the session you should: – Understand what bits/bytes/words are. – Understanding conversion between.
Lecture 6: Floating Point Number Representation Information Representation: Floating Point Number Representation Lecture # 7.
Numbering Systems and Conversion Understand How Computing Devices Work 1.
Number Systems. There are 10 kinds of people in the world, those who understand binary and those who don’t.
Binary & Decimal numbers
Introduction To Number Systems
Negative numbers: Week 10 Lesson 1
Negative Binary Numbers
Instructor: Alexander Stoytchev
BINARY CODE.
Boolean Algebra, Bitwise Operations
Negative Binary Numbers
Location in course textbook
Number System conversions
Number Systems and Bitwise Operation
Binary Addition & Subtraction
1. Number Systems.
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Multiplying & Dividing by Powers of Ten
Computer Organization and Design
Instructor: Alexander Stoytchev
Number Systems.
Instructor: Alexander Stoytchev
Number Systems created by: S.Shahrukh haider
Binary Addition (1 of 2) Two 1-bit values A B A + B 1
Binary to Decimal Conversion
ECE 331 – Digital System Design
Multiplying and Dividing Decimals by 10, 100, 1000
Introduction To Number Systems
Bit Manipulations CS212.
1. Number Systems Chapt. 2.
Review Previously in: Lots of language features: functions, lists, records, tuples, variants, pattern matching Today: No new language features New idioms.
Presentation transcript:

Tutorial 7

add_bin_list : bool list list -> bool list Week 7 Exercise 1 Implement a function add_bin_list that adds a list of binary numbers.  add_bin_list : bool list list -> bool list example: add_bin_list [ [true; true; false; true]; [false; false; true]; [true; false; true]] = [false; false; true; false; true; false; false]

Least significant bit (LSB) Most significant bit (MSB) Week 7 Exercise 1 Implement a function add_bin_list that adds a list of binary numbers.  add_bin_list : bool list list -> bool list example: add_bin_list [ [true; true; false; true]; [false; false; true]; [true; false; true]] = [false; false; true; false; true; false; false] Least significant bit (LSB) Most significant bit (MSB)

Least significant bit (LSB) Most significant bit (MSB) Week 7 Exercise 1 1011 Implement a function add_bin_list that adds a list of binary numbers.  add_bin_list : bool list list -> bool list example: add_bin_list [ [true; true; false; true]; [false; false; true]; [true; false; true]] = [false; false; true; false; true; false; false] Least significant bit (LSB) Most significant bit (MSB)

From Lecture let add_3 b1 b2 b3 = match b1, b2, b3 with | true, true, true -> true, true | true, true, false | true, false, true | false, true, true -> true, false | true, false, false | false, true, false | false, false, true -> false, true | false, false, false -> false, false;; let rec add_bin' n1 n2 carry = match n1, n2 with | [], [] -> [carry] | [], n2 -> add_bin' [false] n2 carry | n1, [] -> add_bin' n1 [false] carry | d1 :: d1s, d2 :: d2s -> let (carry, d) = add_3 d1 d2 carry in d :: (add_bin' d1s d2s carry);; let rec add_bin n1 n2 = add_bin' n1 n2 false;;

solution let add_bin_list = List.fold_left add_bin [false];; val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b list -> ’a List.fold_left f a [b1; ...; bn] is f (... (f (f a b1) b2) ...) bn.

mul_bin_pow : bool list -> int -> bool list Week 7 Exercise 2 Write a function mul_bin_pow n k which multiplies a binary number n by the number k-th power of 2.  mul_bin_pow : bool list -> int -> bool list For example:  mul_bin_pow [true; true; false; true] 3 = [false; false; false; true; true; false; true]

solution Multiplying a number by the k-th power of its base Shift the digits/bits k places to the left Pad with zeros Example: in decimal multiplying any number by 10 555 *10 =5550 The same with binary: multiplying any number by 2, shift bits to the left and add zero to the LSB 111 *(1000)=111000 2^3

solution let rec nfalse = function | 0 -> [] | n -> false :: (nfalse (n - 1));; let mul_bin_pow n k = (nfalse k) @ n;;

mul_bin : bool list -> bool list -> bool list Week 7 Problem Implement the function mul_bin which multiplies two binary numbers.  mul_bin : bool list -> bool list -> bool list For example: mul_bin [false; true; true] [true; false; true] = [false; true; true; true; true]

solution let rec bin_to_pows n = function | [] -> [] | true :: ns -> n :: (bin_to_pows (n + 1) ns) | false :: ns -> (bin_to_pows (n + 1) ns);; val bin_to_pows : int -> bool list -> int list = <fun> let shift bs ps = List.map (fun k -> mul_bin_pow bs k) ps;; val shift : bool list -> int list -> bool list list = <fun> let rec mul_bin m n = add_bin_list (shift m (bin_to_pows 0 n));; val mul_bin : bool list -> bool list -> bool list = <fun>