CS 7B - Spring Assignment: Adapting the calculator for bitwise expressions. due 2/21/18

Similar documents
CSE 20 DISCRETE MATH. Fall

Discrete Mathematics. (c) Marcin Sydow. Sets. Set operations. Sets. Set identities Number sets. Pair. Power Set. Venn diagrams

C++ For Science and Engineering Lecture 13

CS1800: Hex & Logic. Professor Kevin Gold

Note: The area of logic that deals with propositions is called the propositional calculus or propositional logic.

2. Write a recursive function to add the first n terms of the alternating harmonic series:

1. Write a program to calculate distance traveled by light

CS 7B - Fall Assignment 4: Exploring Wumpus (Linked Rooms). Due 12/13/18

MiniMat: Matrix Language in OCaml LLVM

We are here. Assembly Language. Processors Arithmetic Logic Units. Finite State Machines. Circuits Gates. Transistors

CS Exam 1 Study Guide and Practice Exam

Introduction to Z3. Bow-Yaw Wang. December 19, Institute of Information Science Academia Sinica, Taiwan

Chapter 4 Number Representations

Number System. Decimal to binary Binary to Decimal Binary to octal Binary to hexadecimal Hexadecimal to binary Octal to binary

4 Switching Algebra 4.1 Axioms; Signals and Switching Algebra

Compiling Techniques

Boolean Logic. CS 231 Dianna Xu

Types and Programming Languages (15-814), Fall 2018 Assignment 4: Data Representation (Sample Solutions)

1 Boolean Algebra Simplification

Symbolic Logic Outline

INTRODUCTION. This is not a full c-programming course. It is not even a full 'Java to c' programming course.

Chapter 2: Princess Sumaya Univ. Computer Engineering Dept.

ECE/CS 250 Computer Architecture

ECE 250 / CPS 250 Computer Architecture. Basics of Logic Design Boolean Algebra, Logic Gates

Digital Logic (2) Boolean Algebra

CS 7B - Fall Final Exam (take-home portion). Due Wednesday, 12/13 at 2 pm

T Reactive Systems: Temporal Logic LTL

Boolean algebra. Examples of these individual laws of Boolean, rules and theorems for Boolean algebra are given in the following table.

Recursion. Recursion. Steven Zeil. November 25, 2013

Outline. EECS150 - Digital Design Lecture 4 - Boolean Algebra I (Representations of Combinational Logic Circuits) Combinational Logic (CL) Defined

Computer Organization: Boolean Logic

An Introduction to Z3

Module 2. Basic Digital Building Blocks. Binary Arithmetic & Arithmetic Circuits Comparators, Decoders, Encoders, Multiplexors Flip-Flops

Structural Induction

CS 163/164 - Exam 1 Study Guide and Practice Exam

Outline. policies for the first part. with some potential answers... MCS 260 Lecture 10.0 Introduction to Computer Science Jan Verschelde, 9 July 2014

CSCE 222 Discrete Structures for Computing. Propositional Logic. Dr. Hyunyoung Lee. !!!!!! Based on slides by Andreas Klappenecker

Introduction to ANTLR (ANother Tool for Language Recognition)

20. Combinational Circuits

Congratulations you're done with CS103 problem sets! Please evaluate this course on Axess!

What is Logic? Introduction to Logic. Simple Statements. Which one is statement?

Introduction to Computer Programming, Spring Term 2018 Practice Assignment 1 Discussion:

BOOLEAN ALGEBRA. Introduction. 1854: Logical algebra was published by George Boole known today as Boolean Algebra

ENS Lyon Camp. Day 5. Basic group. C October

Definition 2. Conjunction of p and q

System Data Bus (8-bit) Data Buffer. Internal Data Bus (8-bit) 8-bit register (R) 3-bit address 16-bit register pair (P) 2-bit address

Proposition/Statement. Boolean Logic. Boolean variables. Logical operators: And. Logical operators: Not 9/3/13. Introduction to Logical Operators

Lecture 5: Arithmetic

CS1800 Discrete Structures Spring 2018 February CS1800 Discrete Structures Midterm Version A

1 Short adders. t total_ripple8 = t first + 6*t middle + t last = 4t p + 6*2t p + 2t p = 18t p

Introduction to Computer Programming, Spring Term 2018 Practice Assignment 1 Discussion:

Boolean Algebra and Digital Logic

Notater: INF3331. Veronika Heimsbakk December 4, Introduction 3

import java. u t i l. ;... Scanner sc = new Scanner ( System. in ) ;

Round 5: Hashing. Tommi Junttila. Aalto University School of Science Department of Computer Science

A crash course in Digital Logic

UNIVERSITI TENAGA NASIONAL. College of Information Technology

Error Detection and Correction: Hamming Code; Reed-Muller Code

CprE 281: Digital Logic

Unit 8A Computer Organization. Boolean Logic and Gates

Computer Science. 20. Combinational Circuits. Computer Science COMPUTER SCIENCE. Section

Tasks of lexer. CISC 5920: Compiler Construction Chapter 2 Lexical Analysis. Tokens and lexemes. Buffering

Fields in Cryptography. Çetin Kaya Koç Winter / 30

Four Important Number Systems

Today s Topics. Methods of proof Relationships to logical equivalences. Important definitions Relationships to sets, relations Special functions

Compiler Principles, PS4

CSE 20 Discrete Math. Algebraic Rules for Propositional Formulas. Summer, July 11 (Day 2) Number Systems/Computer Arithmetic Predicate Logic

Finite Fields. SOLUTIONS Network Coding - Prof. Frank H.P. Fitzek

1) Let h = John is healthy, w = John is wealthy and s = John is wise Write the following statement is symbolic form

ENGIN 112 Intro to Electrical and Computer Engineering

Turing Machines Part Three

Discrete Mathematical Structures. Chapter 1 The Foundation: Logic

CS1800 Discrete Structures Fall 2016 Profs. Aslam, Gold, Ossowski, Pavlu, & Sprague 27 October, CS1800 Discrete Structures Midterm Version A

Math 13, Spring 2013, Lecture B: Midterm

Boolean algebra. Values

Operations on Sets. Gazihan Alankuş (Based on original slides by Brahim Hnich et al.) August 6, 2012

EECS150 - Digital Design Lecture 19 - Combinational Logic Circuits : A Deep Dive

Lecture 2 Review on Digital Logic (Part 1)

Lecture 4 Modeling, Analysis and Simulation in Logic Design. Dr. Yinong Chen

CS 220: Discrete Structures and their Applications. Propositional Logic Sections in zybooks

MA/CSSE 474 Theory of Computation

ECE 302: Probabilistic Methods in Electrical Engineering

EECS150 - Digital Design Lecture 4 - Boolean Algebra I (Representations of Combinational Logic Circuits)

Getting Connected. Chapter 2, Part 2. Networking CS 3470, Section 1 Sarah Diesburg

Combinational Logic Design Principles

Chapter 2: Switching Algebra and Logic Circuits

Congratulations you're done with CS103 problem sets! Please evaluate this course on Axess!

Lecture on Sensor Networks

Logic and Propositional Calculus

6.001 Recitation 22: Streams

UNIT-I: Propositional Logic

Shared on QualifyGate.com

CS100: DISCRETE STRUCTURES. Lecture 5: Logic (Ch1)

10/14/2009. Reading: Hambley Chapters

Constructors - Cont. must be distinguished by the number or type of their arguments.

Switches: basic element of physical implementations

Timo Latvala. February 4, 2004

1.1 Statements and Compound Statements

2. Sets. 2.1&2.2: Sets and Subsets. Combining Sets. c Dr Oksana Shatalov, Spring

COMP 120. For any doubts in the following, contact Agam, Room. 023

Transcription:

CS 7B - Spring 2018 - Assignment: Adapting the calculator for bitwise expressions. due 2/21/18 Background Theory A bitwise number is a number in base 2. In base 2, place values are either a 1 or 0, depending on whether or not that power of two is in the sum to the number: For example, the decimal number 12 is written: 12 10 = 8 10 + 4 10 = 1 2 3 + 1 2 2 + 0 2 1 + 0 2 0 = 1100 2 See 25.5.1: Bits and bit operations for a great description of bitwise operations and their precedence. Also, http://www.cprogramming.com/tutorial/bitwise_operators.html. To be sure, the precedence is also listed at http://en.cppreference.com/w/cpp/language/operator_precedence precedence operator Description 1 ~ bitwise NOT 2 & Bitwise AND 3 ^ Bitwise XOR (exclusive or) 4 Bitwise OR (inclusive or) The interesting difference with the four function (*,/,+,-) calculator developed in chapter 6 and the bitwise calculator we develop here is that there are no right-associative operations: no left to right operations as in + and -, left to right. It s a strict hierarchy with only one operation at each level. Also, there s one more level. Here s a first attempt at a grammar defining the syntax of our input before writing a program that implements the rules of that grammar. AndExpression: Primary AndExpression "&" primary XOrExpression: AndExpression XOrExpression " " AndExpression IOrExpression: XOrExpression IOrExpression " " XOrExpression Primary: Number "(" AndExpression ")" Number Number: Integer Let s look at how to adapt Stroustrup s calculator to evaluate bitwise expressions like this one, which only uses bytes: 8 6 10& 6 = 00001000 00000110 00001010& 00000110 = 00001000 00000110 00001010&11111001 = 00001000 00000110 00001000 = 00001000 00001110 = 00001110

CS 7B Assignment: Adapting the calculator for bitwise expressions. - Page 2 of 5 due 2/21/18 1. Change the Token to have int values (2 bytes) instead of double values...but limit the output to 4 bits using bitset<4>(value) 2. Change all the types of the various functions from double to int, accordingly. 3. Change the operators to handle bitwise operations. For this you will need all new functions. Keep primary() but scrap expression() and term() and replace these with iorexp(), xorexp() and andexp(). Here s some starter code: 1 #i n c l u d e s t d l i b f a c i l i t i e s. h #i n c l u d e <b i t s e t > 3 5 const char number = 8 ; 7 const char p r i n t = ; ; const char q u i t = q ; 9 11 13 i n t iorexp ( ) ; i n t xorexp ( ) ; 15 i n t andexp ( ) ; i n t primary ( ) ; 17 c l a s s Token 19 p u b l i c : char kind ; // what kind o f token 21 i n t value ; // f o r numbers : a value Token ( char ch ) // make a Token from a char 23 : kind ( ch ), value ( 0 ) Token ( char ch, i n t v a l ) // make a Token from a char and a double 25 : kind ( ch ), value ( v a l ) ; 27 29 c l a s s Token stream 31 p u b l i c : Token stream ( ) ; // make a Token stream that reads from c i n 33 Token get ( ) ; // get a Token ( get ( ) i s d e f i n e d e l s e w h e r e ) void putback ( Token t ) ; // put a Token back 35 p r i v a t e : bool f u l l ; // i s t h e r e a Token i n the b u f f e r? 37 Token b u f f e r ; // here i s where we keep a Token put back using putback ( ) ; 39 41 // The c o n s t r u c t o r j u s t s e t s f u l l to i n d i c a t e that the b u f f e r i s empty : 43 Token stream : : Token stream ( ) : f u l l ( f a l s e ), b u f f e r ( 0 ) // no Token in b u f f e r 45 47 49 // The putback ( ) member f u n c t i o n puts i t s argument back i n t o the Token stream s b u f f e r : void Token stream : : putback ( Token t ) 51 i f ( f u l l ) e r r o r ( putback ( ) i n t o a f u l l b u f f e r ) ; 53 b u f f e r = t ; // copy t to b u f f e r f u l l = t r u e ; // b u f f e r i s now f u l l

CS 7B Assignment: Adapting the calculator for bitwise expressions. - Page 3 of 5 due 2/21/18 55 57 59 Token Token stream : : get ( ) 61 i f ( f u l l ) // do we a l r e a d y have a Token ready? // remove token from b u f f e r 63 f u l l=f a l s e ; r e t u r n b u f f e r ; 65 67 char ch ; c i n >> ch ; // note that >> s k i p s whitespace ( space, newline, tab, e t c. ) 69 switch ( ch ) 71 c a s e ; : // f o r p r i n t c a s e q : // f o r q u i t 73 c a s e ( : c a s e ) : c a s e : c a s e ˆ : c a s e & : c a s e : r e t u r n Token ( ch ) ; // l e t each o p e r a t i o n c h a r a c t e r r e p r e s e n t i t s e l f 75 c a s e 0 : c a s e 1 : c a s e 2 : c a s e 3 : c a s e 4 : c a s e 5 : c a s e 6 : c a s e 7 : c a s e 8 : c a s e 9 : 77 c i n. putback ( ch ) ; // put d i g i t back i n t o the input stream 79 i n t v a l ; c i n >> v a l ; // read a f l o a t i n g point number 81 r e t u r n Token ( number, v a l ) ; // number i s the const char 8 83 d e f a u l t : e r r o r ( Bad token ) ; 85 87 89 91 93 Token stream t s ; // p r o v i d e s get ( ) and putback ( ) // d e a l with numbers, p a r e n t h e s e s and 2 s complement 95 i n t primary ( ) 97 Token t = t s. get ( ) ; 99 c a s e ( : // handle ( e x p r e s s i o n ) 101 i n t d = iorexp ( ) ; t = t s. get ( ) ; 103 i f ( t. kind!= ) ) e r r o r ( ) expected ) ; r e t u r n d ; 105 c a s e number : // number i s the const char 8 107 r e t u r n t. value ; // r e t u r n the number s value c a s e : // handle negation 109 r e t u r n primary ( ) ; d e f a u l t : 111 e r r o r ( primary expected ) ; 113 115 117 // d e a l with i n t iorexp ( ) // i n c l u s i v e or e x p r e s s i o n 119

CS 7B Assignment: Adapting the calculator for bitwise expressions. - Page 4 of 5 due 2/21/18 // a s s i g n l e f t the output o f xorexp ( ) ; 121 // c r e a t e a Token, t, and a s s i g n i t the output o f t s. get ( ) 123 c a s e : // r e t u r n the i n c l u s i v e or o f l e f t with the output o f iorexp ( ) ; 125 d e f a u l t : 127 // r e t u r n l e f t ; 129 131 133 // d e a l with ˆ i n t xorexp ( ) 135 // a s s i g n l e f t the output o f andexp ( ) 137 // c r e a t e a Token, t, and a s s i g n i t the output o f t s. get ( ) 139 c a s e ˆ : // r e t u r n the e x c l u s i v e or o f l e f t with the output o f xorexp ( ) 141 d e f a u l t : 143 // r e t u r n l e f t ; 145 147 149 i n t andexp ( ) // a s s i g n l e f t the output o f primary ( ) ; 151 // c r e a t e a Token, t, and a s s i g n i t the output o f t s. get ( ) 153 c a s e & : // r e t u r n the and o f l e f t with the output o f andexp ( ) ; 155 d e f a u l t : 157 // r e t u r n l e f t ; 159 161 163 i n t main ( ) t r y 165 i n t v a l = 0 ; 167 while ( c i n ) Token t = t s. get ( ) ; 169 i f ( t. kind == q u i t ) break ; 171 i f ( t. kind == p r i n t ) cout << = << b i t s e t <4>(val ) << \n ; 173 e l s e t s. putback ( t ) ; 175 v a l = iorexp ( ) ; 177 catch ( e x c e p t i o n& e ) 179 c e r r << e r r o r : << e. what ( ) << \n ; r e t u r n 1 ; 181 catch (... ) 183 c e r r << Oops : unknown e x c e p t i o n! \ n ; r e t u r n 2 ;

CS 7B Assignment: Adapting the calculator for bitwise expressions. - Page 5 of 5 due 2/21/18 185 4. Do a check/expect to see that you get these sorts of correct output: ~8; =0111 ~(3 8); =0100 ~3&~8; =0100 3 8^11; =0011 3 8^11&15; =0011 3 8^11&15 8; =1011 3 8^11&15 8&9; =1011 5. Use the bitwise calculator to test demorgan s laws: [p q] p& q, (1) [p&q] p q. (2)