CSCI 2212: Intermediate Programming / C Chapter 15

Similar documents
Beginning C Programming for Engineers

Signed Binary Numbers

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

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

CS 241 Data Organization Binary

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

COMP2611: Computer Organization. Data Representation

Expression and Operator

C expressions. (Reek, Ch. 5) 1 CS 3090: Safety Critical Programming in C

Computer Organization & Systems Exam I Example Questions

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

Chapter 3 Basic Data Types. Lecture 3 1

CHAPTER 1 Numerical Representation

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

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

Number System. Introduction. Decimal Numbers

Bitwise Instructions

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

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

CS 107 Lecture 2: Bits and Bytes (continued)

LAB A Translating Data to Binary

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

CS 253. January 14, 2017

1010 2?= ?= CS 64 Lecture 2 Data Representation. Decimal Numbers: Base 10. Reading: FLD Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Solutions - Homework 2 (Due date: October 4 5:30 pm) Presentation and clarity are very important! Show your procedure!

Survey. Motivation 29.5 / 40 class is required

Arithmetic and Bitwise Operations on Binary Data

Shift and Rotate Instructions

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

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

Level ISA3: Information Representation

Number Representations

Arithmetic and Bitwise Operations on Binary Data

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

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

Engineering Computing I

EE292: Fundamentals of ECE

Lecture 2: Number Systems

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

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Binary representation of integer numbers Operations on bits

Bits, Words, and Integers

Representation of Information

Chapter 3: Operators, Expressions and Type Conversion

Binary Representations and Arithmetic

IT 1204 Section 2.0. Data Representation and Arithmetic. 2009, University of Colombo School of Computing 1

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: Bits and Bytes and Numbers

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

Number representations

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

Integer Multiplication and Division

Modbus/TCP is supported on some controllers. See QCI-AN028 Modbus TCP.

The Design of C: A Rational Reconstruction

Integer Representation

ELECTRICAL AND COMPUTER ENGINEERING DEPARTMENT, OAKLAND UNIVERSITY ECE-278: Digital Logic Design Fall Notes - Unit 4. hundreds.

CS & IT Conversions. Magnitude 10,000 1,

Microcontroller Architecture and Interfacing HOMEWORK 3

The Design of C: A Rational Reconstruction"

Chapter 10 Error Detection and Correction 10.1

ELECTRICAL AND COMPUTER ENGINEERING DEPARTMENT, OAKLAND UNIVERSITY ECE-2700: Digital Logic Design Winter Notes - Unit 4. hundreds.

UNIT- 3 Introduction to C++

Appendix. Bit Operators

Microcomputers. Outline. Number Systems and Digital Logic Review

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

Lecture Notes on Ints

Topic Notes: Bits and Bytes and Numbers

COMP2121: Microprocessors and Interfacing. Number Systems

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

Number Systems Using and Converting Between Decimal, Binary, Octal and Hexadecimal Number Systems

Applied Computer Programming

Bits, Bytes and Integers

Chapter 4. Operations on Data

Work relative to other classes

Arithmetic Operations

Assist. Prof. Dr. Caner ÖZCAN

Data Representation 1

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

DLD VIDYA SAGAR P. potharajuvidyasagar.wordpress.com. Vignana Bharathi Institute of Technology UNIT 1 DLD P VIDYA SAGAR

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

Chapter 2 Number System

JAVA OPERATORS GENERAL

Binary Values. CSE 410 Lecture 02

Introduction to Computer Science-103. Midterm

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

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

bytes per disk block (a block is usually called sector in the disk drive literature), sectors in each track, read/write heads, and cylinders (tracks).

Lecture 13 Bit Operations

srl - shift right logical - 0 enters from left, bit drops off right end note: little-endian bit notation msb lsb "b" for bit

Time (self-scheduled): Location Schedule Your Exam: What to bring:

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

COMP Overview of Tutorial #2

Binary Arithmetic Intro to Assembly Language CS 64: Computer Organization and Design Logic Lecture #3

Midterm Exam Review Slides

Digital Systems and Binary Numbers

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

Slide Set 1. for ENEL 339 Fall 2014 Lecture Section 02. Steve Norman, PhD, PEng

Logic Circuits I ECE 1411 Thursday 4:45pm-7:20pm. Nathan Pihlstrom.

History of Computing. Ahmed Sallam 11/28/2014 1

Transcription:

... /34 CSCI 222: Intermediate Programming / C Chapter 5 Alice E. Fischer October 9 and 2, 25

... 2/34 Outline Integer Representations Binary Integers Integer Types Bit Operations Applying Bit Operations Count the Bits Reverse the Bit Order Calculate the Parity of the Bits

... 3/34 Integer Representations I. Integer Representations Binary Integers Input Conversion Signed and Unsigned Integers Integer Notation Writing Numbers in Hex Formats for Unsigned Numbers

... 4/34 Integer Representations Binary Integers Representation - a Review Everything in a computer is represented by bits. All integers are stored in binary, even when we write them in base 8 or or 6. Place values for base 6 (hexadecimal) are shown on the left; base-2 (binary) place values are on the right. Each hexadecimal digit occupies the same memory space as four binary bits because 2 4 = 6. 6 3 = 496 6 2 = 256 6 = 6 6 = 2 5 32768 2 3 892 2 248 2 9 52 2 7 28 2 5 32 2 3 8 2 2 2 4 6384 2 2 496 2 24 2 8 256 2 6 64 2 4 6 2 2 4 2

... 5/34 Integer Representations Binary Integers Input Conversion: When scanf() reads a number, the ASCII representation of that number must be converted to binary for internal storage. Suppose we have a file buffer that contains two numbers. The first one has been read already and we are ready to read in the second number with %i format. Here is what happens:. Skip whitespace, initialize answer to. 2. Loop until scanner is NOT pointing at a digit. 2. Multiply the previous answer by 2.2 Calculate the integer corresponding to the ascii character by subtracting. 2.3 Add the value of the digit to the answer. 2.4 Increment the scanner.

... 6/34 Integer Representations Binary Integers Input Conversion: 2 Initially: 2 2 9 5 3 6 \ scanner? answer while (isdigit (*scanner)) { answer *= ; answer += (*scanner++ - ); } Step: Step2: 2 2 9 5 3 6 \ scanner 2 2 9 5 3 6 \ scanner answer 3 answer

... 7/34 Integer Representations Binary Integers Input Conversion: 3 2 2 9 5 3 6 \ scanner 2 2 9 5 3 6 \ scanner 2 2 9 5 3 6 \ scanner 3 3 answer 3 36 answer 36 answer

... 8/34 Integer Representations Integer Types Long, Short, and Int The c99 standard defines integers in four lengths. Int can be the same as short or long. Char can be either signed or unsigned. Common Name Full Name Normal Length char signed char byte unsigned char short signed short int 2 bytes unsigned short unsigned short int int signed int 2 or 4 bytes unsigned unsigned int long signed long int 4 bytes unsigned long unsigned long int long long signed long long int 4 or 8 bytes unsigned short unsigned long long int

... 9/34 Integer Representations Integer Types Signed and Unsigned Integers In a signed integer, the leftmost bit is the sign bit. A in this position has a large negative value. The other positions have positive values, as shown. The binary representations of several signed and unsigned integers follow. Several of these values turn up frequently during debugging, so it is useful to be able to recognize them. 2 5 = 32768 2 4 = 6384 2 3 = 892 2 2 = 496 2 = 248 2 = 24 2 9 = 52 2 8 = 256 2 7 = 28 2 6 = 64 2 5 = 32 2 4 = 6 2 3 = 8 2 2 = 4 2 = 2 2 = Interpreted as a signed short int high-order bit = -32768 Interpreted as an unsigned short int high-order bit = 32768 32767-32768 + 32767 = - -32768 + 22768 = - -32768 + = -32768-32768 + = -32767 32767 +32768 + 32767 = 65535 +32768 + 22768 = 55536 +32768 + = 32768 +32768 + = 32769

... /34 Integer Representations Integer Types Using Unsigned Types Sometimes a program must deal directly with hardware components or the actual pattern of bits stored in the memory. Applications include Cryptography, where you work with bits, not numbers. Random number generation Programs that control hardware switches Sometimes the data is naturally unsigned, as in digital photography.

... /34 Integer Representations Integer Types Integer Notation C provides more than one way to write an integer number because: Base- notation is very inconvenient for many applications that rely on unsigned numbers. Base does not correspond in an easy way to the internal representation of numbers or to their length in bytes. It takes calculation starting with a base- number, to arrive at its binary representation.

... 2/34 Integer Representations Integer Types Integer Literals There are four ways to write a number in your program (a literal): Decimal integer: Use digits 9 but DO NOT start with. Octal integer: Use digits 7 and always start with. Hexadecimal integer: Use digits 9 and A F or a f and always start with x. Hexadecimal character constant: Start with x, followed by one or two digits 9 and A F or a f and end with. Remember: there are two hex digits per byte in an integer value.

... 3/34 Integer Representations Integer Types Writing Numbers in Hex These numbers are signed short ints. Leading zeros may be either written or omitted. Decimal Hex Decimal Hex Decimal Hex x 7 x 65 x4 7 x7 8 x2 9 x5b 8 x8 3 xe 27 x7f xa 32 x2 28 x8 xb 33 x2 255 xff 5 xf 63 x3f 256 x 6 x 64 x4 32767 x7fff - xffff -2 xfffe -32768 x8

... 4/34 Integer Representations Integer Types Conversion Specifiers for Unsigned Values int long short char Character I/O %c Decimal I/O %u %lu %hu Hex input %i or %x %li or %lx %hi or %hx Hex output %4x or %8x %8lx %4hx %x If the input stream contains numbers in the form of hexadecimal literals: x35, they can be read as hex values using %i. If the x is not part of the input, then %x must be used to read a number as hexadecimal.

... 5/34 Bit Operations II. Bit Operations

... 6/34 Bit Operations Bitwise operators. Arity Symbol Meaning Precedence Unary Bitwise complement 5 Binary << Left shift >> Right shift & Bitwise AND 8 ˆ Bitwise exclusive OR (XOR) 7 Bitwise OR 6

... 7/34 Bit Operations Bitwise Complement Arithmetic negation, logical not, and bitwise complement are three ways to get th opposite of a number. Complement inverts all the bits in the number. To negate a number, complement and add. Decimal Hex Binary Decimal Hex Binary x x x x x xff x 2 xfe -x x -x xff!x x!x x x 5 xf x xf6 x 6 x9 x 9 x9 -x 5 xa -x xa!x x!x x

... 8/34 Bit Operations Bitwise And Use AND to turn bits off or to isolate one part of a number. Binary Hex Binary Hex x xff x x7a mask x2 mask xf x & mask x2 x & mask x7 x x7a x x7a mask xc mask x3 x & mask x8 x & mask x2 You will need the last mask in the steganography program, to isolate pairs of bits from the modified image.

... 9/34 Bit Operations Bitwise Or Use or to turn on bits or to combine parts into a whole. Binary Hex Binary Hex x x x xc mask x2 mask xf x mask x2 x mask xfc x x3e x x9c mask xa7 bits x x mask xbf x bits x9d In the steganography program, you need the last operation to make a byte out of four pairs of bits extracted from the modified photograph.

... 2/34 Bit Operations Exclusive Or Use xor to toggle some of the bits or to test for inequality. Binary Hex Binary Hex x xff x xcc mask x3 mask xf x ^ mask xcf x ^ mask x3c x x7a x x7a y xa3 y x7a x ^ y xd9 x ^ y x

... 2/34 Bit Operations Left Shift Left shift moves the bits of the number to the left; bits that move off the left end are forgotten. bits are pulled in to fill the right end. This is very fast for hardware. Shifting left by one position has the same effect on a number as multiplying by 2 but it is a lot faster for the machine. The table on the next slide shows the results of several shift operations on these variables: signed char s; // A one-byte signed integer. unsigned char u; // A one-byte unsigned integer. Note that he result of n << 2 is four times the value of n, as long as no significant bits fall off the left end.

... 22/34 Bit Operations Left and Right Shifts Signed Decimal Hex Binary s 5 xf s << 2 6 x3c s >> 2 3 x3 s xf6 s << 2 4 xd8 s >> 2 3 xfd Unsigned Decimal Hex Binary u xa u << 2 4 x28 u >> 2 2 x2 u 255 xff u << 2 252 xfc u >> 2 63 x3f

... 23/34 Bit Operations Right Shift The table on the previous slide shows the results of several right shift operations. Right shift moves the bits of the number to the right; bits that move off the right end are forgotten. In a signed right shift, copies of the sign bit are pulled in to fill the left end. For an unsigned right shift, bits are used to fill the left end Shifting right by one position has the same effect on a number as dividing by 2. To enable this, a signed right shift keeps the sign bit unchanged. Note that he result of n >> 2 is one fourth of the value of n.

... 24/34 Applying Bit Operations II. Applying Bit Operations Count the Bits Reverse the Bit Order Calculate the Parity of the Bits

... 25/34 Applying Bit Operations Count the Bits Count the Bits Steps and 2 These drawings go with the popcount3() function in the bitops program. The goal is to count the number of bits in the byte. We execute the function popcount3(x49), which is the bitstring. Initially, count=. st time x The result of an AND is if both bits are. Otherwise it is. x & count += x& 2nd time x >>+ x & count += x& Using as a mask keeps the rightmost bit and zeros out the other seven.

... 26/34 Applying Bit Operations Count the Bits Count the Bits Steps 3 through 5 3rd time x >>+ x & If x ends in a bit, the count is not changed. If it ends in a bit, we add to the bit count. count += x& 4th time x x & Now the second bit has been shifted to the end of the word, so the count grows. count += x& 5th time x >>+ x & count += x& 2 2 Using as a mask keeps the rightmost bit and zeros out the other seven.

... 27/34 Applying Bit Operations Count the Bits Count the Bits Steps 6 through finish 6th time x >>+ x & If x ends in a bit, the count is not changed. If it ends in a bit, we add to the bit count. count += x& 2 7th time x x & Now the second bit has been shifted to the end of the word, so the count grows. count += x& 3 End of loop x >>+ count 3 X has become so we leave the loop and return the count.

... 28/34 Applying Bit Operations Reverse the Bit Order Reverse the Bit Order These drawings go with the Reverse program. The goal is to take a byte and reverse the order of its bits so that the last bit becomes the first bit, etc. The code was written to reverse the bits in all the bytes in a pixel image. The variable p points at the current pixel. p We will trace the reversal process on this pixel, x59.

... 29/34 Applying Bit Operations Reverse the Bit Order First Three Steps. c r st time c & r = (r << ) (c & ) c >>= r 2nd time c & r = (r << ) (c & ) c >>= r 3rd time c & r = (r << ) (c & )

... 3/34 Applying Bit Operations Reverse the Bit Order Steps Four through Six. c >>= r 4th time c & r = (r << ) (c & ) c >>= r 5th time c & r = (r << ) (c & ) c >>= r 6th time c & r = (r << ) (c & )

... 3/34 Applying Bit Operations Reverse the Bit Order Ending the Reversal Loop. c >>= r 7th time c & r = (r << ) (c & ) c >>= r 8th time c & r = (r << ) (c & ) p Original contents of *p p after *p = r

... 32/34 Applying Bit Operations Calculate the Parity of the Bits Parity Steps and 2 These drawings go with the parity3() function in the bitops program. The goal is to calculate whether a byte has odd or even bit parity. We execute the function parity3(x49), which is the bitstring. st time x The result of an XOR is if the bits are parity the same, if they are different. parity ^= x 2nd time x >> parity The rightmost bit of parity will change every time x is shifted and the last bit of x is a. parity ^= x

... 33/34 Applying Bit Operations Calculate the Parity of the Bits Parity Steps 3 through 5 3rd time x >> parity If there are an odd number of bits, we call it "odd parity". parity ^= x 4th time x >> parity If there are an even number of bits, we call it "even parity". parity ^= x 5th time x >> parity parity ^= x If the rightmost bit of x is a, parity does not change.

... 34/34 Applying Bit Operations Calculate the Parity of the Bits Parity Steps 6 through finish 6th time x >> parity parity ^= x 7th time x >> parity parity ^= x When we finish, only the rightmost bit will matter; the others will be masked off. We are done when the last bit is shifted off the end of x. Finish x >> parity parity & The value of x is now zero, so we leave the while loop. Then we mask off all the bits of parity except the final one and return the answer. In this case, we have odd parity.