Understanding Bitwise Operations and Shift Operators

bitwise conditional comma size of n.w
1 / 21
Embed
Share

Learn about bitwise operations, logical operators, shift operators, and examples of how they work with binary numbers. Explore how to use bitwise operators for tasks like setting or clearing specific bits, shifting bits left or right, and more.

  • Bitwise Operations
  • Shift Operators
  • Logical Operators
  • Binary Numbers
  • Programming

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Bitwise, Conditional, Comma, Size-of

  2. Bitwise Operators Bitwise Logical Operators Bitwise Shift Operators Ones Complement operator 2

  3. Bitwise Logical operators &(AND),|(OR),^(EXOR) op1 op2 & | ^ 1 1 1 1 0 These are binary operators and require two integer operands. 1 0 0 1 1 0 1 0 1 1 These work on their operands bit by bit starting from LSB (rightmost bit). 0 0 0 0 0 3

  4. Example Suppose x = 10, y = 15 z = x & y sets z=10 like this 0000000000001010 x 0000000000001111 y 0000000000001010 z = x & y Same way |,^ according to the table are computed. 4

  5. Bitwise Shift operators << ,>> These are used to move bit patterns either to the left or right. They are used in the following form op<<n or op>>n Here, op is the operand to be shifted and n is number of positions to shift. 5

  6. Bitwise Shift operator: << << causes all the bits in the operand op to be shifted to the left by n positions. The leftmost n bits in the original bit pattern will be lost and the rightmost n bits that are vacated are filled with 0 s 6

  7. Bitwise Shift operator: >> >> causes all the bits in operand op to be shifted to the right by n positions. The rightmost n bits will be lost and the left most vacated bits are filled with 0 s if number is unsigned integer 7

  8. Examples Suppose X is an unsigned integer whose bit pattern is 0000 0000 0000 1011 x<<1 0000 0000 0001 0110 x>>1 0000 0000 0000 0101 Add ZEROS Add ZEROS 8

  9. Examples Suppose X is an unsigned integer whose bit pattern is 0000 0000 0000 1011 whose equivalent value in decimal number system is 11. x<<3 0000 0000 0101 1000 = 88 x>>2 0000 0000 0000 0010 = 2 Add ZEROS Add Z EROS Note: x=y<<1; same as x=y*2 (Multiplication) x=y>>1; same as x=y/2 (Division) 9

  10. Bitwise Shift operators Op and n can be constants or variables. There are 2 restrictions on the value of n n cannot be ve n should not be greater than number of bits used to represent Op.(E.g.: suppose op is int and size is 2 bytes then n cannot be greater than 16). 10

  11. Bitwise complement operator The complement operator(~) is an unary operator and inverts all the bits represented by its operand. Suppose x=1001100010001111 ~x=0110011101110000 (complement) Also called as 1 s complement operator. 11

  12. Bitwise Operator Examples Output: 6 31 6 -9 #include <stdio.h> int main() { int a=6, b=14; // variable declarations int x=23,y=10; // variable declarations int m=12, n=10; // variable declarations int k=8; printf("The output of the Bitwise AND operator a&b is %d",a&b); printf("The output of the Bitwise OR operator x|y is %d",x|y); printf("The output of the Bitwise ExOR operator m^n is %d",m^n); printf("The output of the Bitwise Comp operator k is %d",~k); return 0; } 12

  13. Bitwise Operator Examples // C Program to demonstrate use of bitwise operators #include <stdio.h> int main() { // a = 5(00000101), b = 9(00001001) unsigned char a = 5, b = 9; Output: a = 5, b = 9 a&b = 1 a|b = 13 a^b = 12 ~a = -6 b<<1 = 18 b>>1 = 4 // The result is 00000001 printf("a = %d, b = %d\n", a, b); printf("a&b = %d\n", a & b); // The result is 00001101 printf("a|b = %d\n", a | b); // The result is 00001100 printf("a^b = %d\n", a ^ b); // The result is 11111010 printf("~a = %d\n", a = ~a); // The result is 00010010 printf("b<<1 = %d\n", b << 1); // The result is 00000100 printf("b>>1 = %d\n", b >> 1); return 0; 13 }

  14. Important:: The bitwise operators should not be used in place of logical operators. The result of logical operators (&&, || and !) is either 0 or 1, but bitwise operators return an integer value. Also, the logical operators consider any non-zero operand as 1. For example: int main() { int x = 2, y = 5; (x & y) ? printf("True ") : printf("False "); (x && y) ? printf("True ") : printf("False "); return 0; } Output: False True 14

  15. Important:: The & operator can be used to quickly check if a number is odd or even. The value of expression (x & 1) would be non-zero only if x is odd, otherwise the value would be zero. The ~ operator should be used carefully. The result of ~ operator on a small number can be a big number if the result is stored in an unsigned variable. And the result may be a negative number if the result is stored in a signed variable int main() { unsigned int x = 1; printf("Signed Result %d \n", ~x); printf("Unsigned Result %ud \n", ~x); return 0; } Output: Signed Result -2 Unsigned Result 4294967294d 15

  16. Sizeof Operator: When sizeof() is used with the data types such as int, float, char etc it simply returns the amount of memory is allocated to that data types. When sizeof() is used with the expression, it returns size of the expression. int main() { int a = 0; double d = 10.21; printf("%lu", sizeof(a + d)); return 0; } Output: ?? 16

  17. Comma (, ,) operator The comma operator is a binary operator that evaluates its first operand and discards the result, it then evaluates the second operand and returns this value (and type). int i = (5, 10); // Value of i will be assigned as 10 The comma operator has the lowest precedence of any C operator. The comma operator is used basically to separate expressions. i = 0, j = 10; // in initialization [ l r ] The meaning of the comma operator in the general expressione1, e2 is evaluate the sub expression e1, then evaluate e2; the value of the expression is the value of e2 . 17

  18. Comma (, ,) operator The comma operator is a binary operator that evaluates its first operand and discards the result, it then evaluates the second operand and returns this value (and type). int main() { int x = 10; int y = 15; printf("%d", (x, y)); return 0; } Output?? 18

  19. Comma (, ,) operator int main() { int x = 10; int y = (x++, ++x); printf("%d", y); return 0; } Output: 12 int main() { int x = 10, y; y = (x++, printf("x = %d\n", x), ++x, printf("x = %d\n", x), x++); printf("y = %d\n", y); printf("x = %d\n", x); return 0; } Output: x = 11 x = 12 y = 12 x = 13 19

  20. Comma (, ,) operator The following expression in the code: a = 2, 3, 4; is evaluated as: (((a = 2), 3), 4); This is because the reason that assignment operator has high precedence over the comma operator. Check the output for following: 1. int a = 4, 3; 2. int a; a = 4,3; 3. int a =(4, 3); //3 will be assigned //Error //4 will be assigned 20

  21. The conditional operator (? :) condition ? expression1 : expression2 condition is an expression that is evaluated first. If the result of the evaluation of condition is TRUE (nonzero), then expression1 is evaluated and the result of the evaluation becomes the result of the operation. If condition is FALSE (zero), then expression2 is evaluated and its result becomes the result of the operation. maxValue = ( a > b ) ? a : b; Equivalent to: if ( a > b ) else maxValue = a; maxValue = b; 21

Related


More Related Content