CS33 Project Gear Up. Data

Similar documents
Project Data: Manipulating Bits

Arithmetic and Bitwise Operations on Binary Data

COMP 122/L Lecture 2. Kyle Dewey

Arithmetic and Bitwise Operations on Binary Data

Beginning C Programming for Engineers

Arithmetic Operations

Chapter 4. Operations on Data

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

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

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Integers II. CSE 351 Autumn 2018

ANITA S SUPER AWESOME RECITATION SLIDES. 15/18-213: Introduction to Computer Systems Bit Logic and Floating Point, 27 January 2014 Anita Zhang

CSE 303 Lecture 18. Bitwise operations. reading: Programming in C Ch. 12. slides created by Marty Stepp

Lecture Topics. Announcements. Today: Integer Arithmetic (P&H ) Next: continued. Consulting hours. Introduction to Sim. Milestone #1 (due 1/26)

Arithmetic and Logical Operations

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

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

LAB A Translating Data to Binary

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

Data III & Integers I

Midterm 1 topics (in one slide) Bits and bitwise operations. Outline. Unsigned and signed integers. Floating point numbers. Number representation

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

1/31/2017. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

9/2/2016. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

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

Arithmetic Operators. Portability: Printing Numbers

Data III & Integers I

Chapter 3: Arithmetic for Computers

CS 64 Week 1 Lecture 1. Kyle Dewey

Chapter 3 Basic Data Types. Lecture 3 1

COMP2611: Computer Organization. Data Representation

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

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

Arithmetic Logic Unit (ALU)

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

15213 Recitation 2: Floating Point

Data III & Integers I

Unit-2 (Operators) ANAND KR.SRIVASTAVA

CS 356, Fall 2018 Data Lab (Part 2): Manipulating Bits Due: Mon, Sep. 17, 11:59PM

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

Computer Architecture and Organization: L04: Micro-operations

4/8/17. Admin. Assignment 5 BINARY. David Kauchak CS 52 Spring 2017

Integers II. CSE 351 Autumn Instructor: Justin Hsia

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

CSE I-Sem)

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

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

CS 2505 Fall 2018 Data Lab: Data and Bitwise Operations Assigned: November 1 Due: Friday November 30, 23:59 Ends: Friday November 30, 23:59

Lecture 13 Bit Operations

Chapter 03: Computer Arithmetic. Lesson 09: Arithmetic using floating point numbers

CS61B Lecture #14: Integers. Last modified: Wed Sep 27 15:44: CS61B: Lecture #14 1

BITWISE OPERATORS. There are a number of ways to manipulate binary values. Just as you can with

CS61B Lecture #14: Integers

Organisasi Sistem Komputer

ENCE 3241 Data Lab. 60 points Due February 19, 2010, by 11:59 PM

Number Systems Standard positional representation of numbers: An unsigned number with whole and fraction portions is represented as:

Floating-Point Data Representation and Manipulation 198:231 Introduction to Computer Organization Lecture 3

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

ECE 30 Introduction to Computer Engineering

CS 261 Fall Mike Lam, Professor Integer Encodings

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Unsigned Binary Integers

Unsigned Binary Integers

Integer Representation

CSCI 2212: Intermediate Programming / C Chapter 15

Binary representation of integer numbers Operations on bits

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

CS 253. January 14, 2017

The Design of C: A Rational Reconstruction

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

CS 356, Fall 2018 Data Lab (Part 1): Manipulating Bits Due: Wednesday, Sep. 5, 11:59PM

JAVA OPERATORS GENERAL

The Design of C: A Rational Reconstruction"

Binary Values. CSE 410 Lecture 02

Floating Point. The World is Not Just Integers. Programming languages support numbers with fraction

Few reminders and demos

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

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

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

OCR H446 A-Level Computer Science

CS 105 Lab 1: Manipulating Bits

ECE232: Hardware Organization and Design

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

FALL 2017 CSCI 304 LAB2 (Due on October-10, 11:59:59pm)

CS356: Discussion #2 Integer Operations. Marco Paolieri

15213 Recitation Section C

Floating Point Numbers

Floating Point Numbers

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

COMP Primitive and Class Types. Yi Hong May 14, 2015

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Topic Notes: Bits and Bytes and Numbers

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

Register Transfer Language and Microoperations (Part 2)

CSCI2467: Systems Programming Concepts

CS-281, Spring 2011 Data Lab: Manipulating Bits Assigned: Jan. 17, Due: Mon., Jan. 31, 11:59PM

Bitwise Data Manipulation. Bitwise operations More on integers

Lecture 8: Addition, Multiplication & Division

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

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

Transcription:

CS33 Project Gear Up Data

Project Overview You will be solving a series of puzzles using your knowledge of data representations.

IMPORTANT: Collaboration This project has a different collaboration policy than most projects in CS 33. Please review it! As a result, this Gear Up Session will be a little different -- we will not discuss any implementations of any of the code you will write. Come ask us questions after if you re unsure.

Topics You Should Review Data representation (lecture slides, ch. 2 textbook) Integers (Two s Complement) Bitwise operations Bit masks and sign extension Floating point

Roadmap Read the Worked Example to get an idea of the problems you will be solving Read the stencil carefully to know limitations on operators you can use for each puzzle Solve each of the 9 puzzles in bits.c cs0330_install data Use the provided testers to test your solutions as you go

Demos! Testers

Testers btest In the directory for the project, run make btest;./btest Tests functionality, does not check if you followed the coding rules dpc In the directory for the project, run./dpc bits.c Checks that your code follows the rules for each puzzle, and counts the number of operators driver.pl In the directory for the project, run./driver.pl Combines dpc and btest to check correctness and performance of your solution

Logical Operators logical NOT A T F NOT A F T

Logical Operators (C) logical NOT:! A!A non-zero 00...00 zero 00..01

Bitwise Operators (C) bitwise NOT: ~ A ~A 11 00 10 01 01 10 00 11

Logical Operators logical AND A B A AND B T T T T F F F T F F F F

Logical Operators (C) logical AND: && A B A && B non-zero non-zero 1 non-zero 0 0 0 non-zero 0 0 0 0

Bitwise Operators (C) bitwise AND: & A B A & B 1 1 1 1 0 0 0 1 0 0 0 0 Example: 1100 & 1010 = 1000

Logical Operators logical OR A B A OR B T T T T F T F T T F F F

Logical Operators (C) logical OR: A B A B non-zero non-zero 1 non-zero 0 1 0 non-zero 1 0 0 0

Bitwise Operations (C) bitwise OR: A B A B 0 0 0 0 1 1 1 0 1 1 1 1 Example: 1100 1010 = 1110

Logical Operators logical XOR A B A XOR B T T F T F T F T T F F F

Bitwise Operations (C) bitwise XOR: ^ A B A ^ B 0 0 0 0 1 1 1 0 1 1 1 0 Example 1100 ^ 1010 = 0110

Two s Complement Representation The last bit (all the way on the left) is 0 if the number is positive, and 1 if it is negative To calculate the decimal representation for a w-bit word size (bi is the bit at index i):

Two s Complement Examples For a 4-bit word size 0000 0*2 3 + 0*2 2 + 0*2 1 + 0*2 0 = 0 1000-1*2 3 + 0*2 2 + 0*2 1 + 0*2 0 = -8 1111

Two s Complement Values Values to keep in mind: All 0 s is 0 All 1 s is -1 0 followed by all 1 s is the most positive integer 1 followed by all 0 s is the most negative integer

Bit Shifts (C) Shifting the bits in some representation either left or right Left Shift: a << b Right Shift: a >> b

Left Shift: << (C) a << b 1. Shift a left b bits, throwing away leading bits. a. How many bits are thrown away? 2. Fill in right bits with 0s. a. How many bits are 0s?

Left Shift: Examples For a 4-bit word size 1 << 1 = 2 0001 << 1 = 0010 1 << 3 = 8 0001 << 3 = 1000 7 << 2 = 12 0111 << 2 = 1100 8 << 1 = 0 1000 << 1 = 0000

Right Shift: >> (C) a >> b 1. Shift a right b bits, throwing away trailing bits. a. How many bits are thrown away? 2. Logical shift or arithmetic shift? a. Logical: Fill in left bits with 0s. i. Used for unsigned integers b. Arithmetic: Fill in left bits with sign bit. i. Used for signed integers

Right Shift: Examples For a 5-bit word size, using two s complement... 00001 >> 1 unsigned (logical): 1 >> 1 = 00000 = 0 signed (arithmetic): 1 >> 1 = 00000 = 0 00111 >> 2 unsigned (logical): 7 >> 2 = 00001 = 1 signed (arithmetic): 7 >> 2 = 00001 = 1 11001 >> 2 unsigned (logical): 25 >> 2 = 00110 = 6 signed (arithmetic): -7 >> 2 = 11110 = -2

Bit Masking A bit mask is an integer whose binary representation is intended to combine with another value using &,, or ^ to extract or set a particular bit or set of bits

Bit Masking with & mask = 00000001 value1 = 10011011 value2 = 10011100 mask & value1 == 00000001 mask & value2 == 00000000

Bit Masking with mask = 00101000 value = 11000101 mask value == 11101101

Bit Masking with ^ mask = 11111111 value = 10101010 mask ^ value == 01010101

Multiplication using << You can multiply a number by a power of 2 by using left-shift (<<). Ex: 4*(2^3) = 00000100 << 3 = 00100000 = 32

Dividing using >> You can use >> to divide an integer by a power of 2 Positive integers Ex: 48/(2^4) = 00110000 >> 4 = 00000011 = 3 Ex: 48/(2^5) = 00110000 >> 5 = 00000001 = 1 Throwing away trailing bits rounds down Negative integers Ex: -48/(2^4) = 11010000 >> 4 = 11111101 = -3 Ex: -48/(2^5) = 11010000 >> 5 = 11111110 = -2 WRONG! Just using >> on a negative number rounds away from zero (bad)

Correct Rounding for Negative Ints Problem: we want to increment our answer only in the case where there s rounding away from 0-48 / (2^5) = 11010000 >> 5 = 11111111 Only if there is a one in the trailing bits that will be cut off (ie: the answer will be rounded), then the leading bits need to be incremented by 1. How tho?? Hint: check the lecture slides ;)

Tips Try to follow the process used in the worked example. Understanding the lectures on bit manipulation and two s complement will be very important for this project Making a bit mask using the sign bit will be very useful (x >> 31) Come to TA hours if you have questions! TAs will try to help without giving away the answer

So Why Am I Doing This? You will gain a better understanding of bit-level representation of data. You will gain a better understanding of how the computer computes functions like negating a number or checking if numbers are equal. You will understand the various bit-level operations

Questions?