Lecture12. Outline Binary representation of integer numbers Operations on bits –The Bitwise AND Operator –The Bitwise Inclusive-OR Operator –The Bitwise.

Slides:



Advertisements
Similar presentations
The Binary Numbering Systems
Advertisements

Binary Logic (review) Basic logical operators: (Chapter 7 expanded)
Floating Point Numbers
Introduction to C Programming CE Lecture 8 Bitwise Operations.
1 Lecture 3 Bit Operations Floating Point – 32 bits or 64 bits 1.
Chapter 4 Operations on Bits

24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05.
Floating Point Numbers
Floating Point Numbers
Bit Operations C is well suited to system programming because it contains operators that can manipulate data at the bit level –Example: The Internet requires.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
Binary Operations Math/Logical. Binary Math Decimal Addition Example ) Add = 15 Write down 5, carry ) Add 3 +
Bitwise Operations CSE 2451 Rong Shi. Working with bits – int values Decimal (not a power of two – used for human readability) – No preceding label –
Operations on data CHAPTER 4.
4 Operations On Data Foundations of Computer Science ã Cengage Learning.
Binary numbers and arithmetic. ADDITION Addition (decimal)
Simple Data Type Representation and conversion of numbers
Computer Organization and Architecture Computer Arithmetic Chapter 9.
Computer Arithmetic Nizamettin AYDIN
1 Lecture 5 Floating Point Numbers ITEC 1000 “Introduction to Information Technology”
Review CAS CS210 Ying Ye Boston University. Logical expressions Truth table input: A, B, Coutput: D ABCD (~A)(~B)(~C)
NUMBER REPRESENTATION CHAPTER 3 – part 3. ONE’S COMPLEMENT REPRESENTATION CHAPTER 3 – part 3.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Information Representation: Negative and Floating Point.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
ECE232: Hardware Organization and Design
1 Programming in Machine Language SCSC 311 Spring 2011.
Number Systems Spring Semester 2013Programming and Data Structure1.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 10 Department of Computer Science and Software Engineering University of.
Data Representation in Computer Systems
S. Rawat I.I.T. Kanpur. Floating-point representation IEEE numbers are stored using a kind of scientific notation. ± mantissa * 2 exponent We can represent.
CH09 Computer Arithmetic  CPU combines of ALU and Control Unit, this chapter discusses ALU The Arithmetic and Logic Unit (ALU) Number Systems Integer.
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.
Bits and Bytes. BITWISE OPERATORS Recall boolean logical operators in Java… boolean logical operators: &, |, ^ not: ! Show truth tables.
CSC 221 Computer Organization and Assembly Language
Bitwise operators. Representing integers We typically think in terms of decimal (base 10) numbers.  Why?  A decimal (or base 10) number consists of.
CSC 270 – Survey of Programming Languages C Lecture 5 – Bitwise Operations and Operations Miscellany.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Information Representation: Floating Point Representation.
Bitwise operators. Representing integers We typically think in terms of decimal (base 10) numbers.  Why?  A decimal (or base 10) number consists of.
1 Exam / Homework Exam 1 in Class 10 –Open book / open notes HW3 due next class HW4 will be on-line soon. Finishing Chapter 2 of K&R. We will go through.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Numbers in Computers.
CS 232: Computer Architecture II Prof. Laxmikant (Sanjay) Kale Floating point arithmetic.
DATA TYPE, MEMORY, AND FUNCTION Dong-Chul Kim BioMeCIS UTA 2/18/
Lecture 6: Floating Point Number Representation Information Representation: Floating Point Number Representation Lecture # 7.
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language.
Binary Logic (review) Basic logical operators:(Chapter 7 expanded) NOT AND – outputs 1 only if both inputs are 1 OR – outputs 1 if at lest one input is.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
Binary Addition The simplest arithmetic operation in binary is addition. Adding two single-digit binary numbers is relatively simple, using a form of carrying:
1 CE 454 Computer Architecture Lecture 4 Ahmed Ezzat The Digital Logic, Ch-3.1.
Floating Point Representations
Programming and Data Structure
Chapter 4 Operations on Bits.
Data Structures Mohammed Thajeel To the second year students
What to bring: iCard, pens/pencils (They provide the scratch paper)
Data Representation Data Types Complements Fixed Point Representation
ECEG-3202 Computer Architecture and Organization
Shift & Rotate Instructions)
Shift & Rotate Instructions)
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
Module 10 Operations on Bits
靜夜思 床前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。 ~ 李白 李商隱.
Bit Manipulations CS212.
Lecture 9: Shift, Mult, Div Fixed & Floating Point
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

Lecture12

Outline Binary representation of integer numbers Operations on bits –The Bitwise AND Operator –The Bitwise Inclusive-OR Operator –The Bitwise Exclusive-OR Operator –The Ones Complement Operator –The Left Shift Operator –The Right Shift Operator –Binary representation of floating-point numbers Review: Operators: The complete table of operator's precedence

Binary representation of integers Positive integers: least significant or low-order bit most significant or high-order bit Sign bit 1*2^0+0*2^1+1*2^2=5

Binary representation of integers Negative integers: 10 least significant or low-order bit most significant or high-order bit Sign bit Steps to convert a negative number from decimal to binary: Ex: add 1 to the value: -5+1=-4 2. express the absolute value of the result in binary: 4= then “complement” all the bits: = Steps to convert a negative number from binary to decimally: Ex: complement all of the bits: convert the result to decimal: 4 3.change the sign of the result, and then subtract 1: -4-1=-5

Bit operators & Bitwise AND | Bitwise Inclusive-OR ^ Bitwise Exclusive-OR ~ Ones complement << Left shift >> Right shift All the operators, with the exception of the ones complement operator ~, are binary operators and as such take two operands. Bit operations can be performed on any type of integer value in C—be it short, long, long long, and signed or unsigned—and on characters, but cannot be performed on floating-point values.

Bitwise AND b1b2b1&b int a=25; int b=77; printf(“%x %x %x”,a,b,a&b); a=0x19 b=0x4d a&b=0x

Exercise: even or odd testing if an integer is even or odd, using bitwise operations: the rightmost bit of any odd integer is 1 and of any even integer is 0. int n; if ( n & 1 ) printf(“odd”); else printf(“even”);

Bitwise Inclusive-OR (OR) b1b2b1|b int a=25; int b=77; printf(“%x %x %x”,a,b,a|b); a=0x19 b=0x4d a|b=0x5d

Bitwise vs boolean ! AND: bitwise: &, boolean: && OR: bitwise: |, boolean: || int a,b; a=1; b=2; printf("a:%i b:%i &:%i &&:%i |:%i ||:%i \n", a, b, a&b, a&&b, a|b, a||b);

Bitwise Exclusive-OR (XOR) b1b2b1^b a=0x19 b=0x4d a^b=0x int a=25; int b=77; printf(“%x %x %x”,a,b,a^b);

The Ones Complement Operator b1~b int a=25; printf(“%x %x”,a,~a); a=0x19 ~a=0xffffffe

Usage: Masking bits A mask is a bit pattern with some bits set to on (1) and some bits to off (0). Example: int MASK = 2; // , with only bit number 1 nonzero. Masking bits: flags = flags & MASK; all the bits of flags (except bit 1) are set to 0 because any bit combined with 0 using the & operator yields 0. Bit number 1 will be left unchanged. (If the bit is 1, 1 & 1 is 1; if the bit is 0, 0 & 1 is 0.) This process is called "using a mask" because the zeros in the mask hide the corresponding bits in flags. you can think of the 0s in the mask as being opaque and the 1s as being transparent. The expression flags & MASK is like covering the flags bit pattern with the mask; only the bits under MASK's 1s are visible

Usage: Turning bits on Turn on (set to 1) particular bits in a value while leaving the remaining bits unchanged. Example: an IBM PC controls hardware through values sent to ports. To turn on a device (i.e., the speaker), the driver program has to turn on the 1 bit while leaving the others unchanged. This can be done with the bitwise OR operator. The MASK has bit 1 set to 1, other bits 0 int MASK = 2; // , with only bit number 1 nonzero. The statement flags = flags | MASK; sets bit number 1 in flags to 1 and leaves all the other bits unchanged. This follows because any bit combined with 0 by using the | operator is itself, and any bit combined with 1 by using the | operator is 1.

Usage: Turning bits off Turn off (set to 0) particular bits in a value while leaving the remaining bits unchanged. Suppose you want to turn off bit 1 in the variable flags. The MASK has bit 1 set to 1, other bits 0 int MASK = 2; // , with only bit number 1 nonzero. The statement: flags = flags & ~MASK; Because MASK is all 0s except for bit 1, ~MASK is all 1s except for bit 1. A 1 combined with any bit using & is that bit, so the statement leaves all the bits other than bit 1 unchanged. Also, a 0 combined with any bit using & is 0, so bit 1 is set to 0 regardless of its original value.

Usage: Toggling bits Toggling a bit means turning it off if it is on, and turning it on if it is off. To toggle bit 1 in flag : The MASK has bit 1 set to 1, other bits 0 int MASK = 2; // , with only bit number 1 nonzero. flag = flag ^ MASK; You can use the bitwise EXCLUSIVE OR operator to toggle a bit. The idea is that if b is a bit setting (1 or 0), then 1 ^ b is 0 if b is 1 and is 1 if b is 0. Also 0 ^ b is b, regardless of its value. Therefore, if you combine a value with a mask by using ^, values corresponding to 1s in the mask are toggled, and values corresponding to 0s in the mask are unaltered.

Usage: Checking value of a bit For example: does flag has bit 1 set to 1? you must first mask the other bits in flag so that you compare only bit 1 of flag with MASK : if ((flag & MASK) == MASK)

The Left Shift Operator a : 0x int a=25; printf(“%x %x”,a,a<<1); a<<1 : 0x the bits contained in the value are shifted to the left a number of places (bits) Bits that are shifted out through the high-order bit of the data item are lost, 0 s are always shifted in through the low-order bit of the value.shifted to the left. Associated with this Bit lost

Exercise: multiply with 2 Left shifting has the effect of multiplying the value that is shifted by two. Multiplication of a value by a power of two: left shifting the value the appropriate number of places int a; scanf("%i",&a); printf("a multiplied with 2 is %i \n",a<<1); printf("a multiplied with 4 is %i \n",a<<2);

The Right Shift Operator a : 0x int a=25; printf(“%x %x”,a,a>>1); a>>1 : 0xc Shifts the bits of a value to the right. Bits shifted out of the low-order bit of the value are lost. Right shifting an unsigned value or a signed positive value always results in 0s being shifted in on the left Bit lost

Right shift of negative integers If the sign bit is 1, on some machines 1s are shifted in, and on others 0s are shifted in. This former type of operation is known as an arithmetic right shift, whereas the latter is known as a logical right shift. Never make any assumptions about whether a system implements an arithmetic or a logical right shift. A program that shifts signed values right might work correctly on one system but fail on another due to this type of assumption Bit lost 1 ?

Example: extract groups of bits Example: an unsigned long value is used to represent color values, with the low-order byte holding the red intensity, the next byte holding the green intensity, and the third byte holding the blue intensity. You then want to store the intensity of each color in its own unsigned char variable. unsigned char BYTE_MASK = 0xff; unsigned long color = 0x002a162f; unsigned char blue, green, red; red = color & BYTE_MASK; green = (color >> 8) & BYTE_MASK; blue = (color >> 16) & BYTE_MASK; printf("%x %x %x\n",red, green, blue);

Exercise: getbits getbits(x,p,n): returns the (right adjusted) n -bit field of x that begins at position p. We assume that bit position 0 is at the right end and that n and p are positive values. /* getbits: get n bits from position p */ unsigned getbits(unsigned x,int p, int n) { return (x>>(p+1-n))&~(~0<<n); } n p

Exercise: printbits printbits(x,p,n): prints the n -bit field of x that begins at position p. We assume that bit position 0 is at the right end and that n and p are positive values. void printbits(unsigned x,int p, int n) { int i; unsigned mask=1<<p; for (i=0; i<n; i++) { if (x&mask) printf("1"); else printf("0"); mask=mask>>1; }

Exercise: IEEE float representation Standards: single precision / double precision IEEE Standard 754 Floating Point Numbers in Single precision Layout: sign 1 bit biased exponent 8 bits fraction 23 bits

IEEE float (cont) Sign: 0=positive, 1=negative Exponent: a bias is added to the actual exponent in order to get the stored exponent. For IEEE single-precision floats, this value is 127 Examples: – actual exponent =0: biased exponent field stored =127. – biased exponent field stored = 200: actual exponent =( )= 73. The exponents are powers of 2 in binary ! Mantissa: there is an assumed leading 1 in the actual mantissa that is not stored in memory, so the mantissas are actually 24 bits, even though only 23 bits are stored in the fraction field. Examples: – Stored fraction =11000…0: actual mantissa= …0(binary)= 1+1/2+1/4 Represented value:

IEEE float examples Float representation: Sign=0 Biased exponent= =127 => Actual exponent= =0 Fraction= Mantissa=1.f=1.11=1+1/2+1/4=1.75 Value of number is 2^0*1.75=1.75 Float representation: Sign=0 Biased exponent= =128 => Actual exponent= =1 Fraction= Mantissa=1.f=1.11=1+1/2+1/4=1.75 Value of number is 2^1*1.75=3.5

Exercise: print bits of a float void printbits(unsigned x,int p, int n); int main(void) { float f=3.5; unsigned int *ip=&f; printf("\n sign: ");printbits(*ip,31,1); printf("\n biased exponent: "); printbits(*ip,30,8); printf("\n fraction: "); printbits(*ip,22,23); return 1; }

Review: Operators in C