CSCI 2467, Spring 2018 Class Activity: Two s complement, bitwise and logical operators Monday, February 5

Similar documents
Arithmetic and Bitwise Operations on Binary Data

Arithmetic and Bitwise Operations on Binary Data

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

LAB A Translating Data to Binary

Groups of two-state devices are used to represent data in a computer. In general, we say the states are either: high/low, on/off, 1/0,...

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

Signed Binary Numbers

COMP 122/L Lecture 2. Kyle Dewey

CSCI 2212: Intermediate Programming / C Chapter 15

CSCI2467: Systems Programming Concepts

Bits, Words, and Integers

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

COMP2611: Computer Organization. Data Representation

CDA 3103 Computer Organization Exam 1 (Sep. 22th, 2014)

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

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

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

Bits, Bytes and Integers

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

CS & IT Conversions. Magnitude 10,000 1,

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

1. NUMBER SYSTEMS USED IN COMPUTING: THE BINARY NUMBER SYSTEM

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Bitwise Instructions

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

Data III & Integers I

Chapter 4. Operations on Data

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

Digital Logic. The Binary System is a way of writing numbers using only the digits 0 and 1. This is the method used by the (digital) computer.

Where are we? Compiler. translating source code (C or Java) Programs to assembly language And linking your code to Library code

World Inside a Computer is Binary

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Read this before starting!

CS33 Project Gear Up. Data

ECE 2020B Fundamentals of Digital Design Spring problems, 6 pages Exam Two Solutions 26 February 2014

CS 107 Lecture 2: Bits and Bytes (continued)

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Integer Representation

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

Digital Arithmetic. Digital Arithmetic: Operations and Circuits Dr. Farahmand

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

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

Computer Organization & Systems Exam I Example Questions

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

2. Integers. 9 * celsius / Celsius to Fahrenheit. Precedence. (9 * celsius / 5) + 32

Data III & Integers I

Bits, Bytes and Integers Part 1

Integers II. CSE 351 Autumn 2018

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

2. Integers. 9 * celsius / Celsius to Fahrenheit. Precedence. (9 * celsius / 5) + 32

2. Integers. 9 * celsius / Celsius to Fahrenheit. Precedence. (9 * celsius / 5) + 32

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 1: Basic Concepts. Chapter Overview. Welcome to Assembly Language

Chapter 2 Number System

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

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

ECE 2020B Fundamentals of Digital Design Spring problems, 6 pages Exam Two 26 February 2014

15213 Recitation 2: Floating Point

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

Microcontroller Architecture and Interfacing HOMEWORK 3

Where we are going (today)

Description Hex M E V smallest value > largest denormalized negative infinity number with hex representation 3BB0 ---

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Exercise: Using Numbers

Decimal & Binary Representation Systems. Decimal & Binary Representation Systems

EE 109 Unit 6 Binary Arithmetic

Operators and Type Conversion. By Avani M. Sakhapara Assistant Professor, IT Dept, KJSCE

Read this before starting!

Chapter 3: Operators, Expressions and Type Conversion

Computer Systems C S Cynthia Lee

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

What Is It? Instruction Register Address Register Data Register

CSE I-Sem)

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

Chapter 10 Binary Arithmetics

Data III & Integers I

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

BITS, BYTES, AND INTEGERS

Lecture Notes on Ints

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

CS101 Lecture 04: Binary Arithmetic

CS:APP Web Aside DATA:TNEG: Bit-Level Representation of Two s Complement Negation

Lecture 13 Bit Operations

Bits, Bytes, and Integers

Beginning C Programming for Engineers

UNIT 7A Data Representation: Numbers and Text. Digital Data

Survey. Motivation 29.5 / 40 class is required

CS 64 Week 1 Lecture 1. Kyle Dewey

Representation of Non Negative Integers

gcc o driver std=c99 -Wall driver.c bigmesa.c

Unsigned Binary Integers

Unsigned Binary Integers

Read this before starting!

CS 265. Computer Architecture. Wei Lu, Ph.D., P.Eng.

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

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

Numbers and Representations

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

Java enum, casts, and others (Select portions of Chapters 4 & 5)

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

The Design of C: A Rational Reconstruction

Transcription:

CSCI 467, Spring 08 Class Activity: Two s complement, bitwise and logical operators Monday, February 5 Introduction 467 Instructor: M. Toups mtoups@cs.uno.edu In this activity you will practice working with binary numbers, bitwise operators, and logical operators. This activity is based in part on material developed by Professor Saturnino Garcia of the University of San Diego and is used with permission. Each group member will get a copy of the activity to keep. The group s answers go into the response sheet, which will be turned in.. Group Member Roles We are doing this activity in 3- or 4-person groups, in which each group member has a role (in addition to answering the questions, which all group members will do together). Before you begin, take a minute to review the roles of each group member. Below is a summary of the four roles; write the name of the person taking that role next to the summary, and on the response sheet cover page. If your group only has three members, combine the roles of Facilitator and Process Analyst. For this and all future activities, the Facilitator should also take the role of the reader and read questions aloud to the group. Facilitator: Reads question aloud; keeps track of time and makes sure everyone contributes appropriately. Recorder: Records all answers & questions, and provides team reflection to team & instructor. Spokesperson: Talks to the instructor (or other teams). Compiles and runs programs (when applicable). Process Analyst: Considers and recommends how the team could work and learn more effectively. Role Facilitator Recorder Spokesperson Process Analyst Name

Review of Negative Integers. In a two s complement system, what is the leftmost bit in a non-negative number? What about the leftmost bit in a negative number?. Complete the following table to indicate the most positive (i.e. largest) and most negative (i.e. smallest) number that can be represented with a given number of bits when using two s complement representation. Bits Most positive Most negative 0 - - 3 4 3. Use your answer from the previous question to find an expression that gives the most positive number that can be represented by a N-bit two s complement number. Hint: This will be related to a power of two in some way. 4. Use your answer from the previous question to find an expression that gives the most negative number that can be represented by a N-bit two s complement number. Hint: This will be related to a power of two in some way. 3 Bit-Level Operations In the 800s, George Boole proposed a logic-system based on two values: 0 and. We will work through how these operations are exposed in C, and by extension the underlying system. The first set of operations are performed by treating the integer as a bit-vector. 7. One of the steps to negating a signed integer was to invert all of the bits. This operation, complement or ~, can be applied to any integer. For each integer, convert it from hexadecimal to binary and then apply the operation. Show the binary version and then convert the answer back to hexidecimal. Example: ~(0xF0F0) ~( 0000 0000) 0000 0000 0x0F0F ~(0x3C3C) ~(0xCAFE) ~(0x0000)

8. Fill in the following table. In the left column is a value (let s call it z). Convert it from decimal to binary, then use bitwise AND to show (z & 0x): z (Dec) z (Bin) z & 0x - 0 0000-0 0000 0000 9. For which numbers was the value & 0x not 0000? What is a common property of these integers? 0. Many times in systems programming, we want to test if a flag value is set in a given bitpattern. The programmer will commonly use (X & FLAG) == FLAG. Give an explanation of this expression. When does it return true ()? When does it return false (0)?. Systems programmers will also regularly have to combine flag values. Describe how OR ( ) is used in the following example, which creates a new file for writing: open(filename, (O_WRONLY O_CREAT O_TRUNC),...); (Hint: O_WRONLY, O_WRONLY, and O_WRONLY are each flags, meaning only one bit is set to in each, at a different bit position.) 3

. We know that in signed integer datatypes (such as int and short int in C) the leftmost bit (or most-significant bit, MSB) is the sign bit. Let s say you want to write an expression in C which masks out all bits in x except the sign bit. Fill in the blank in this expression with the correct constant value to use as a bitmask: int signbit = x & ; 3. Now consider the situation, like in Data Lab, where you cannot use arbitrarily large constants. Only constants 0 through 55 (0x00 through 0xff) are allowed. Now, how would you write the code (like above) that masks out all bits in x except the sign bit? (Hint: utilize bit-shift operators) int signbit = x & ; 4. In both cases above (which should yield the same value), what is the value of signbit if x is positive? Negative? 5. Let s say you need to convert signbit from above into a value that is always either 0 or. Fill in the blank below to complete the C expression, in terms of signbit: (This one is tricky. Remember how arithmetic shifts work!) int isnegative = ( signbit >> ) & ; Could you also write this expression in terms of x? int isnegative = ( x >> ) & ; The blanks above should be filled with either binary or logical operators, or constants. 4

4 Logical Operations This section will explore logical operations. These operations contrast with bit-level in that they treat the entire value as a single element. In other languages, the type of these values would be termed, bool or boolean. C does not have any such type. Instead, the value of 0 is false and all other values are true. The three operators are AND (&&), OR ( ), and NOT (!).! is commonly termed bang.. With 4-bit values, how many values are false and how many are true? (Keep in mind there are 6 total values when using 4 bits). Evaluate the following expression: (0x3 && 0xC) == (0x3 & 0xC) Is it true () or false (0)? (Hint: Pay attention to the logical && and the bitwise & ) Is this result what you expected? Why or why not? 3. Test whether!!x == X holds across different values of X. X!X!!X - 0 4. Now, for each of the previous values, substitute complement (~) for logical not. Do these results differ from the previous results? How? Why? X ~X ~~X - 0 5