Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Memory and the Character Display Chapter 10. The Bitwise Operators The bitwise operators are used to access the computer hardware. Use of bitwise operators."— Presentation transcript:

1 Memory and the Character Display Chapter 10

2 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

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

4 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 = 10001001 – b = 01001000 – a&b = 00001000

5 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(); }

6 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

7 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 = 10001001 – b = 01001000 – a|b = 11001001

8 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

9 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 = 10001001 – b = 01001000 – a^b = 11000001

10 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

11 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 = 10001001 – ~a = 01110110

12 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

13 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 = 10001001 – a<<5 = 00100000

14 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

15 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 = 10001001 – a>>5 = 00000100

16 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

17 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

18 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;

19 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"); }

20 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"); }


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

Similar presentations


Ads by Google