CSC 270 – Survey of Programming Languages C Lecture 5 – Bitwise Operations and Operations Miscellany.

Slides:



Advertisements
Similar presentations
B261 Systems Architecture
Advertisements

©Brooks/Cole, 2003 Chapter 4 Operations on Bits. ©Brooks/Cole, 2003 Apply arithmetic operations on bits when the integer is represented in two’s complement.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05.
Lecture 02 Data Types, Conversions if Statements METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan.
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
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.
Professor Jennifer Rexford COS 217
1 MATERI PENDUKUNG OPERATOR Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations System-oriented Programming, B. Hirsbrunner,
Unsigned and Signed Numbers. Hexadecimal Number 217A 16 Position Digits A Value = 2x x x16 + Ax1 = 2x x x16.
Bitwise Operations CSE 2451 Rong Shi. Working with bits – int values Decimal (not a power of two – used for human readability) – No preceding label –
Computers Organization & Assembly Language
Summer 2014 Chapter 1: Basic Concepts. Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, Chapter Overview Welcome to Assembly Language.
1 Homework Turn in HW2 tonight HW3 is on-line already Questions?
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
C Operators. CONTENTS CONDITIONAL OPERATOR SIMPLE ASSIGNMENT OPERATOR COMPOUND ASSIGNMENT OPERATOR BITWISE OPERATOR OPERATOR PRECEDENCE.
Bits and Bytes. BITWISE OPERATORS Recall boolean logical operators in Java… boolean logical operators: &, |, ^ not: ! Show truth tables.
Basic Operators. What is an operator? using expression is equal to 9. Here, 4 and 5 are called operands and + is the operator Python language supports.
Lecture12. Outline Binary representation of integer numbers Operations on bits –The Bitwise AND Operator –The Bitwise Inclusive-OR Operator –The Bitwise.
C++ Basics Tutorial 6 Operators. What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..)
Bitwise Operators Fall 2008 Dr. David A. Gaitros
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
15-Nov-15 All the Operators. operators.ppt 2 Precedence An operator with higher precedence is done earlier (precedes) one with lower precedence A higher.
1 Operators and Expressions. Expressions Combination of Operators and Operands Example 2 * y + 5 Operands Operators.
1 Workshop Topics - Outline Workshop 1 - Introduction Workshop 2 - module instantiation Workshop 3 - Lexical conventions Workshop 4 - Value Logic System.
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
CSE 351 Number Representation & Operators Section 2 October 8, 2015.
Module 5 JavaScript Operators. CS346 Javascript-52 Examples  JS-5 Examples.
Expressions and Operators in C. Expressions and Operators Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); Two types: – Function calls – The expressions.
Operators & Expressions
CSE 351 Number Representation. Number Bases Any numerical value can be represented as a linear combination of powers of n, where n is an integer greater.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Expression and Operator. Expressions and Operators u Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); u Two types: –Function calls –The expressions formed.
Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.
1 Manipulating Information (1). 2 Outline Bit-level operations Suggested reading –2.1.7~
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Rational Expressions relational operators logical operators order of precedence.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
CSE 220 – C Programming Expressions.
Chapter 4 Operations on Bits.
Operators and Expressions
Expressions and Assignment Statements
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
All the Operators 22-Nov-18.
All the Operators 4-Dec-18.
Chapter-3 Operators.
Bits and Bytes Topics Representing information as bits
Homework Homework Continue Reading K&R Chapter 2 Questions?
Introduction to Programming – 4 Operators
CS 206D Computer Organization
Number Representation & Operators
Comp Org & Assembly Lang
All the Operators 6-Apr-19.
Bitwise Operators.
All the Operators 13-Apr-19.
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
Chap 7. Advanced Control Statements in Java
Expressions and Assignment Statements
靜夜思 床前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。 ~ 李白 李商隱.
Bit Manipulations CS212.
EECE.2160 ECE Application Programming
Presentation transcript:

CSC 270 – Survey of Programming Languages C Lecture 5 – Bitwise Operations and Operations Miscellany

Logical vs. Bitwise Operations Logical operations assume that the entire variable represents either true or false. – Combining two integer values using a logical operator produces one result, with the variable representing true or false. Bitwise operations assume that each bit in the variable’s value represents a separate true or false. – Combining two integer values using a bitwise operators produces 8 (or 16 or 32 or 64) separate bits each representing a true or a false.

Logical Values in C Although the 1999 standard for C includes booleans, it does not exist in older versions. Integers are usually used for boolean values, with nonzero values being accepted as true and zero values being accepted as false. Boolean operations will produce a 1 for true and a 0 for false.

&& - Logical AND The logical AND operator is && and produces a 1 if both operands are nonzero; otherwise, it produces a x &&yyx

&& – An Example #include intmain(void) { unsignedu, v, w, x = 0x10, //(hex 16) y = 0x110, //(hex 272: ) z = 0x0; //(hex 0) u = x && y; v = x && z; w = y && z; printf("u = %x\tv = %x\tw = %x\n", u, v, z); return(0); } Output u = 1 v = 0 w = 0

|| - Logical OR The logical OR operator is || and produces a 1 if either operands is nonzero; otherwise, it produces a 0.

Logical || – An Example #include intmain(void) { unsignedu, v, w, x = 0x10, y = 0x110, z = 0x0; u = x || y; v = x || z; w = y || z; printf("u = %x\tv = %x\tw = %x\n", u, v, z); return(0); } Output u = 1 v = 1 w = 1

Logical NOT The logical NOT operator ! Inverts the value; nonzero becomes 0 and 0 becomes 1.

Logical NOT – An Example #include intmain(void) { unsigned x = 0x110, y; y = !x; // because x has something, it becomes 0 printf("x = ox%x\ty = ox%x\n", x, y); x = 0x0; y = !x; // because x is 0, it becomes 1 printf("x = ox%x\ty = ox%x\n", x, y); return(0); } Output x = ox110 y = ox0 x = ox0 y = ox1

Bitwise Operations Bitwise operations treat the operands as 8-bit (or 16- or 32-bit) operands. Performing a bit- wise AND operation on two 8-bit integers means that 8 AND s are performed on corresponding bits. Example:

Bitwise AND & A bitwise AND operation is actually 8 (or 16 or 32) AND operations. An example of AND ing: The AND instruction can be used to clear selected bits in an operand while preserving the remaining bits. This is called masking. clearedunchanged

Bitwise AND & – An Example binary.htm unsignedu, v, w, x = 0xab87, //( ) y = 0x4633, //( ) z = 0x1111; //( ) u = x & y; v = x & z; w = y & z; printf("u = ox%x\tv = ox%x\tw = ox%x\n", u, v, w); Output u = ox203 v = ox101 w = ox11 ox203 binary (was x & y) ox101 binary (was x & z) ox11 binary (was y & z)

Bitwise OR | A bitwise OR operation is actually 8 (or 16 or 32) OR operations. An example of OR ing: The OR instruction can be used to set selected bits in an operand while preserving the remaining bits. unchanged set

Bitwise OR | – An Example unsignedu, v, w, x = 0xab87, //( ) y = 0x4633, //( ) z = 0x1111; //( ) u = x | y; v = x | z; w = y | z; printf("u = ox%x\tv = ox%x\tw = %oxx\n“, u, v, w); Output u = oxefb7 v = oxbb97 w = ox5733 oxefb7 binary (was x & y) oxbb97 binary (was x & z) ox5733 binary (was y & z)

Bitwise NOT ~ (One’s Complement) The bitwise NOT (better known as the one’s complement) inverts each bit in the operand. Example unsigned x, y = 0xab87; x = ~y; printf("x = %x\ty = %x\n", x, y); Output x = ffff5478 y = ab87 Add 1 to this to make a two’s complement – negative number representation

Bitwise XOR ^ A bitwise XOR operation is actually 8 (or 16 or 32) AND operations. An example of XOR ing: The XOR instruction can be used to reverse selected bits in an operand while preserving the remaining bits. unchangedinverted

Bitwise XOR – An Example unsignedu, v, w, x = 0xab87, y = 0x4633, z = 0x1111; u = x ^ y; v = x ^ z; w = y ^ z; printf("u = %x\tv = %x\tw = %x\n", u, v, w); Output u = edb4 v = ba96 w = 5722

Bit Shifting To make your mask >> Right shifting << Left Shifting x = 0x ff ; // y = x << 8; /* y is 0x ff00 */ // Results may vary depending on the computer – int can be different sizes on different computers. x & ~077 will turn off lowest six bits.

getbits() /* * getbits() - Get n bits from position p */ unsigned getbits(unsigned x, int p, int n) { return ((x >> (p n)) & ~(~0 << n)); } /** if p is 4 and n is 3 – 3 bits from pos 4 x is oxfb ( ) ((oxfb >> (4 + 1 – 3)) & ~(~0 << 3)); (( >> 2 ) & ~( << 3)) (( ) & ~( )) (( ) & ( ))

Assignment Operators An assignment operator is just another operator in C. We can rewrite i = i + 2; as i += 2; or i = i + x * y; as i += x * y; Similarly, there are -=, *=, /=, etc.

Assignment Operators Caution! i *= 2 + y; is rewritten as i = i * (2+y); NOT i = (i *2) + y;

bitcount() /* * bitcount() - Count 1s in x */ int bitcount(unsigned x) { int b; for (b = 0; x != 0; x >>= 1) if (x & 01) b++; return(b); }

Conditional Expressions Why write if (a > b) z = a; else a = b; when you can write z = (a > b)? a : b;

Conditional Expressions The general form is expression1? expression2: expression3; when expression1 is nonzero, expression2 is evaluated. Otherwise expression3 is evaluated. The usual rules of conversion are in effect. inti, j, a, b; floatx; … i = (a > b)? j: x;

Conditional Expressions If this useful? YES!! z = (a > b)? a : b;/* z = max (a, b); */ x = (x > 0)? x : -x;/* x = abs(x) */ /* Print 5 values to a line */ for (i = 0; i < MAXSIZE; i++) printf("%d%c", x[i], i % 5 == 4? '\n':'\t');

Operator Precedence OperatorAssociativity () [] ->. left to right ! ~ unary (type) * (ptr) & (address) sizeof right to left * / % left to right + - left to right > left to right >= left to right == != left to right & (bitwise AND) left to right ^ (bitwise XOR) left to right | (bitwise OR) left to right

Bitwise Summary Bitwise operators act on bits inside the number 1 hex digit = 4 binary digits Mask – the number set for comparison & - used to clear bits – 1 = both true; – 0 in the mask clears and 1 preserves original | - used to set bits – 1= either true; – 1 in the mask sets to 1 and 0 preserves original ^ - used to reverse bits – 1 = only one true; – 1 in the mask reverses bits and 0 preserves

Bitwise Summary ~ one’s complement – reverses every bit Set your mask using shifting  << a number : left shift by the number and zero fill  >> a number : right shift by the number and zero fill  Extra gift – ternary operator – embedded if/else:  (a > b)? j: x; If a is greater than b then replace expression with j, otherwise replace with x.