Memory and the Character Display Chapter 10. The Bitwise Operators The bitwise operators are used to access the computer hardware. Use of bitwise operators.

Slides:



Advertisements
Similar presentations
Senem Kumova Metin CHAPTER 7 Bitwise Operators and Enumeration Types.
Advertisements

Introduction to C Programming CE Lecture 8 Bitwise Operations.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05.
Assignment Operators =, +=, *= A += B means (A+B) --->A or A = (A+B) Similarly true for -=, *=, /=, and %=. The basic rule is from right to left. Never.
Lecture 02 Data Types, Conversions if Statements METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan.
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.
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 –
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
1 CS 162 Introduction to Computer Science Chapter 5 ASCII to Integer Conversion Herbert G. Mayer, PSU Status 11/9/2014.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To be able to use the bitwise logical operators in programs ❏ To be able to use.
1 Homework Turn in HW2 tonight HW3 is on-line already Questions?
 Input and Output Functions Input and Output Functions  OperatorsOperators Arithmetic Operators Assignment Operators Relational Operators Logical Operators.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
Bit Manipulation when every bit counts. Questions on Bit Manipulation l what is the motivation for bit manipulation l what is the binary, hexadecimal,
C Operators. CONTENTS CONDITIONAL OPERATOR SIMPLE ASSIGNMENT OPERATOR COMPOUND ASSIGNMENT OPERATOR BITWISE OPERATOR OPERATOR PRECEDENCE.
Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas ertificación en AVA.
CSC 270 – Survey of Programming Languages C Lecture 5 – Bitwise Operations and Operations Miscellany.
Lecture12. Outline Binary representation of integer numbers Operations on bits –The Bitwise AND Operator –The Bitwise Inclusive-OR Operator –The Bitwise.
Bitwise operators. Representing integers We typically think in terms of decimal (base 10) numbers.  Why?  A decimal (or base 10) number consists of.
Bitwise Operators in C. Bitwise operators are used to manipulate one or more bits from integral operands like char, int, short, long.
Bitwise Operators Fall 2008 Dr. David A. Gaitros
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.
Bit Operations Horton pp Why we need to work with bits Sometimes one bit is enough to store your data: say the gender of the student (e.g. 0.
CS115 FALL Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Number Systems and Bitwise Operation.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Introduction to Computer Organization & Systems Topics: Command Line Bitwise operators COMP Spring 2014 C Part V.
DATA TYPE, MEMORY, AND FUNCTION Dong-Chul Kim BioMeCIS UTA 2/18/
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.
Chapter 8 Bit Operations By C. Shing ITEC Dept Radford University.
August 6, Operators. August 6, Arithmetic Operators.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
ME2008– W05 MID1- Reference 2016Q1- Source: Deitel /C- How To.
CSE 220 – C Programming Expressions.
CSE 220 – C Programming Bitwise Operators.
Week 3 - Friday CS222.
Integer Real Numbers Character Boolean Memory Address CPU Data Types
INC 161 , CPE 100 Computer Programming
Bit Operations Horton pp
Overview Register Transfer Language Register Transfer
Number Systems and Bitwise Operation
Introduction to C Programming
Introduction to Programming and the C Language
Arithmetic operations, decisions and looping
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 14 Bitwise Operators Objectives
Bits and Bytes Topics Representing information as bits
Bits and Bytes Topics Representing information as bits
Introduction to C Programming
Functions, Part 2 of 3 Topics Functions That Return a Value
CS-401 Computer Architecture & Assembly Language Programming
Homework Homework Continue Reading K&R Chapter 2 Questions?
Comp Org & Assembly Lang
Bitwise Operators.
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
Bitwise Operators.
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
Bit Operations Horton pp
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

Memory and the Character Display Chapter 10

The Bitwise Operators The bitwise operators are used to access the computer hardware. Use of bitwise operators requires knowledge of different number systems and conversion numbers from one system into another number system The data inside the computer is represented in binary form, i.e. in sequence of 0s and 1s. 8bits are used to represent on e character inside the computer The bit-wise operators are used to manipulate the data inside the computer. These are also used in networking software to move data from one computer to another in the network

The Bitwise operators OperationsSymbol AND& Inclusive OR| Exclusive OR(XOR)^ Right Shift>> Left Shift<< Complement~

The Bitwise & (AND) Operator The bitwise AND operator is used to compare the content of two operands( of int data type) on bit-by-bit basis. It returns 1 if the corresponding bit in both the operands is 1 otherwise it return 0. For example: – a = – b = – a&b =

Demonstrate bitwise And operator #include main() //demonstrate bitwise and operator { unsigned int x1,x2; while(x1 !=0 || x2!=0) //terminnate on 0,0 { printf("enter two hex number(ff or less):" ); scanf("%x %x",&x1,&x2); printf("%02x & %02x=%02x\n",x1,x2,x1 & x2); } getch(); }

Example Integer a = 23, b= 13 and c= 11 – a&b – a&c – c&b – Convert from decimal to binary – Find a&b, a&c and c&b – Convert back to binary to decimal

The Bitwise | (OR) Operator The bitwise OR operator is used to compare the contents of two operands (of int data type) on bit-by-bit basis. It returns 1 if any one of the two bits is 1, otherwise it returns 0, it represented by vertical bar (|) For example: – a = – b = – a|b =

Example Integer a = 23, b= 13 and c= 11 – a|b – a|c – c|b – Convert from decimal to binary – Find a|b, a|c and c|b – Convert back to binary to decimal

The Bitwise ^ (XOR) Operator The bitwise XOR operator is used to compare the contents of two operands (of int data type) on bit-by-bit basis. It returns 1 if the bit in the corresponding operands are different otherwise it returns 0( if corresponding bits are same). It represented by caret(^) sign For example: – a = – b = – a^b =

Example Integer a = 23, b= 13 and c= 11 – a^b – a^c – c^b – Convert from decimal to binary – Find a^b, a^c and c^b – Convert back to binary to decimal

The Bitwise ~(Complement) Operator The bitwise Complement operator is used to operate on an expression. It inverts the bits expression It takes each bit of the operand and convert it to 1 if it is a 0 and to 0 if it is 1 It represented by tilda(~) sign For example: – a = – ~a =

Example Integer a = 23, b= 13 and c= 11 – ~(a^b) – ~(a|c) – ~(c&b) – Convert from decimal to binary – Find ~(a^b), ~(a|c) and ~(c&b) – Convert back to binary to decimal

The Left-Shift (<<) Operator Left-shift operator is used to shift a specified number of bits of an operand to the left It has two operands, the first operand on the left-hand- side of the operator is the constant or variable whose bits are to be shifted. The second operand on the right-hand- side of the operator specifies the number of bits that are to be shifted to the left The For example: – a = – a<<5 =

Example Integer a = 23, b= 13 – a<<3 – b<<10 – Convert from decimal to binary – Find a<<3 and b<<10 – Convert back to binary to decimal

The Right-Shift (>>) Operator Right-shift operator is used to shift a specified number of bits of an operand to the right It has two operands, the first operand on the left-hand- side of the operator is the constant or variable whose bits are to be shifted. The second operand on the right-hand- side of the operator specifies the number of bits that are to be shifted to the right The For example: – a = – a>>5 =

Example Integer a = 23, b= 13 – a>>3 – b>>10 – Convert from decimal to binary – Find a>>3 and b>>10 – Convert back to binary to decimal

Example Integer a = 23, b= 13 – a<<3 – b<<10 – Convert from decimal to binary – Find a<<3 and b<<10 – Convert back to binary to decimal

Bitwise Calculator #include void pbin(int);//perform bitwise calculation void pline(void); main() { char op[10]; unsigned int x1,x2; while(x1 != 0 || x2 != 0) //terminate on 0 op 0 { printf("\n\n enter expression(example 'ff00 & 1111'):"); scanf("%x %s %x",&x1,op,&x2); printf("\n"); switch( op[0] ) { case '&': pbin(x1); printf("&(and)\n"); pbin(x2); pline(); pbin(x1 & x2); break; case '|': pbin(x1); printf("|(incl or)\n"); pbin(x2); pline(); pbin(x1 | x2); break;

case '^': pbin(x1); printf("^(excl or)\n"); pbin(x2); pline(); pbin(x1 ^ x2); break; case '>': pbin(x1); printf(">>"); printf("%d \n", x2 ); pline(); pbin(x1 >> x2); break; case '<': pbin(x1); printf("<< "); printf("%d\n", x2); pline(); pbin(x1 << x2); break; case '~': pbin(x1); printf("~(complement)\n"); pline(); pbin(~ x1); break; default:printf("not valid operator"); }

void pbin(int num) { int j,bit; unsigned int mask; mask=0x8000; //one bit mask printf("%04x ",num); //printf in hex for(j=0;j<16;j++) //for each bit { bit =(mask & num) ? 1 : 0; //bit is 1 or 0 printf("%d",bit); //printf bit if(j==7) //printf dash between printf("--"); //bites mask >>=1; } printf("\n"); } void pline(void) { printf(" \n"); }