Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ics212-18-bitwiseoperators

Avatar for William Albritton William Albritton
September 29, 2015
200

 ics212-18-bitwiseoperators

Avatar for William Albritton

William Albritton

September 29, 2015
Tweet

Transcript

  1. Memory Allocation  Bitwise AND Operator (&)  Bitwise Inclusive

    OR Operator (|)  Bitwise Exclusive OR Operator (^)  One’s Complement Operator (~)  Left Shift (<<) and Right Shift (>>) Operators  Logical Operators & Bitwise Assignment Operators
  2. Bitwise Operators  All data in a computer is in

    bits (ones and zeros)  Bitwise operators are applied to each bit separately on integer data types:  char, short, int, and long (signed or unsigned)  Usually we use unsigned integers as operands
  3. Bitwise AND Operator (&)  Returns one (1) if both

    bits are one (1), otherwise returns zero (0) Bit x Bit y x & y 1 1 1 1 0 0 0 1 0 0 0 0
  4. Bitwise AND Example int result = number1 & number2; Bit

    pattern: 01001001 10010110 00000010 11010010 11111000 10100100 00110010 11101011 ----------------------------------- 01001000 10000100 00000010 11000010
  5. Masks and Bitwise AND  A mask is used by

    bitwise AND operator (&) to determine if a specific one (1) or a zero (0) is present  Masks are used to select some bits using ones (1s)  A one (1) returns a one (1), if the other bit is one (1)  A one (1) returns a zero (0), if the other bit is zero (0)  Or hide other bits using zeros (0s)  A zero (0) always returns a zero (0), if other bit is 0 or 1
  6. Masks and Bitwise AND Example int result = number &

    mask; Bit pattern: 01001001 10010110 00000010 11010010 00000000 00000000 11111111 11111111 ----------------------------------- 00000000 00000000 00000010 11010010
  7. Masks and Bitwise AND Example II int result = number

    & mask; Bit pattern: 01001001 10010110 00000010 11010010 11111111 11111111 00000000 00000000 ----------------------------------- 01001001 10010110 00000000 00000000
  8. Example Program  See example code:  bitand.c: input two

    numbers, takes bitwise AND, masks  printbits.c: printbit() function definition for 32-bit ints  printbits.h: printbit() function prototype  makefile-bitwise: makefile for bitwise programs % cp bitand.c program.c % make –f makefile-bitwise % ./program
  9. Bitwise Inclusive OR Operator (|)  Returns zero (0) if

    both bits are zero (0), otherwise returns one (1) Bit x Bit y x | y 1 1 1 1 0 1 0 1 1 0 0 0
  10. Bitwise Inclusive OR Example int result = number1 | number2;

    Bit pattern: 01001001 10010110 00000010 11010010 11111000 10100100 00110010 11101011 ----------------------------------- 11111001 10110110 00110010 11111011
  11. Setting Bits with Bitwise Inclusive OR  The bitwise inclusive

    OR (|) operator can be used to set specific bits  Bits are set to one (1) using ones (1s)  One (1) inclusive OR with zero (0) or one (1) is one (1)  The other bits are not changed by using zeros (0s)  Zero (0) inclusive OR with zero (0) is zero (0)  Zero (0) inclusive OR with one (1) is one (1)
  12. Setting Bits Example int result = number | set; Bit

    pattern: 01001001 10010110 00000010 11010010 00000000 00000000 11111111 11111111 ----------------------------------- 01001001 10010110 11111111 11111111
  13. Setting Bits Example II int result = number | set;

    Bit pattern: 01001001 10010110 00000010 11010010 11111111 11111111 00000000 00000000 ----------------------------------- 11111111 11111111 00000010 11010010
  14. Example Program  See example code:  bitior.c: input two

    numbers and use bitwise inclusive OR (|) to set the bits  printbits.c, printbits.h, getdouble.c, getdouble.h  makefile-bitwise: makefile for bitwise programs % cp bitior.c program.c % make –f makefile-bitwise % ./program
  15. Bitwise Exclusive OR Operator (^)  Returns zero (0) if

    both bits are zero (0), or both bits are one (1), otherwise returns one (1) Bit x Bit y x ^ y 1 1 0 1 0 1 0 1 1 0 0 0
  16. Bitwise Exclusive OR Example int result = number1 ^ number2;

    Bit pattern: 01001001 10010110 00000010 11010010 11000101 00100001 10010111 01001111 ----------------------------------- 10001100 10110111 10010101 10011101
  17. Flipping Bits with Bitwise Exclusive OR  Bitwise exclusive OR

    operator (^) can be used to take the one’s complement for specific bits  Bits are “flipped” by using ones (1s)  One (1) exclusive OR with zero (0) is one (1)  One (1) exclusive OR with one (1) is zero (0)  The other bits are not changed by using zeros (0s)  Zero (0) exclusive OR with zero (0) is zero (0)  Zero (0) exclusive OR with one (1) is one (1)
  18. Flipping Bits Example int result = number ^ flip; Bit

    pattern: 01001001 10010110 00000010 11010010 00000000 00000000 11111111 11111111 ----------------------------------- 01001001 10010110 11111101 00101101
  19. Flipping Bits Example II int result = number ^ flip;

    Bit pattern: 01001001 10010110 00000010 11010010 11111111 11111111 00000000 00000000 ----------------------------------- 10110110 01101001 00000010 11010010
  20. Example Program  See example code:  biteor.c: input two

    numbers and use bitwise exclusive OR (^) to “flip” the bits (one’s complement)  printbits.c, printbits.h, getdouble.c, getdouble.h  makefile-bitwise: makefile for bitwise programs % cp biteor.c program.c % make –f makefile-bitwise % ./program
  21. One’s Complement Operator (~)  If bit is one (1),

    returns zero (0)  If bit is one (0), returns one (1) Bit x ~x 1 0 0 1
  22. One’s Complement Example int result = ~number; Bit pattern: 01001001

    10010110 00000010 11010010 ----------------------------------- 10110110 01101001 11111101 00101101
  23. Example Program  See example code:  ones.c: input a

    number and use one’s complement operator (~) to “flip” all the bits  printbits.c, printbits.h, getdouble.c, getdouble.h  makefile-bitwise: makefile for bitwise programs % cp ones.c program.c % make –f makefile-bitwise % ./program
  24. Left Shift Operator (<<)  Shift the bits in A

    to the left B times  Fill in 0’s from the right  Same as multiplying by a power of 2  a << b is equivalent to a * 2b  100 << 5 is equivalent to 100 * 25, which is 3200
  25. Left Shift Example Code int number = 100; int shifter

    = 5; int result = number << shifter; Bit pattern: 00000000 00000000 00000000 01100100 ----------------------------------- 00000000 00000000 00001100 10000000
  26. Left Shift Example Code II int number = -100; int

    shifter = 5; int result = number << shifter; Bit pattern: 11111111 11111111 11111111 10011100 ----------------------------------- 11111111 11111111 11110011 10000000
  27. Right Shift Operator (>>)  Shift the bits in A

    to the right B times  Fill in 0’s for unsigned or positive signed integers  For negative signed integers this is machine dependent, so could be 0s or 1s, depending on the machine
  28. Right Shift Example Code int number = 100; int shifter

    = 5; int result = number >> shifter; Bit pattern: 00000000 00000000 00000000 01100100 ----------------------------------- 00000000 00000000 00000000 00000011
  29. Right Shift Example Code II int number = -100; int

    shifter = 5; int result = number >> shifter; Bit pattern: 11111111 11111111 11111111 10011100 ----------------------------------- 11111111 11111111 11111111 11111100
  30. Example Program  See example code:  shift.c: input a

    number and a shifter to shift to the left and to the right  printbits.c, printbits.h, getdouble.c, getdouble.h  makefile-bitwise: makefile for bitwise programs % cp shift.c program.c % make –f makefile-bitwise % ./program
  31. Example Program  See example code printbits.c  Uses the

    right shift operator (>>) to shift a one (1) 32 times from the leftmost bit to the rightmost bit  Uses the bitwise AND (&) and shifting one (1) as a mask to determine if a zero (0) or a one (1) exists at that position in a number, so the function can output the character zero ('0') or character one ('1')
  32. Logical Operators  Bitwise and logical operators are different 

    Logical operators operate on the variable as a whole, while bitwise operators operate on each bit  Logical AND (&&)  Returns 1 if both variables are not 0, otherwise 0  Logical OR (||)  Returns 1 if at least 1 variable is not 0, otherwise 0
  33. Logical Operators  Logical NOT (!)  If variable is

    non-zero, returns 0  If variables is 0, returns 1
  34. Bitwise Assignment Operators  Bitwise assignment operators have the potential

    for creating bugs in your program, but it shortens your code  A &= B bitwise AND assignment operator  Equivalent to A = A & B  A |= B bitwise inclusive OR assignment operator  Equivalent to A = A | B
  35. Bitwise Assignment Operators  A ^= B bitwise exclusive OR

    assignment operator  Equivalent to A = A ^ B  A <<= B left shift assignment operator  Equivalent to A = A << B  A >>= B right shift assignment operator  Equivalent to A = A >> B
  36. Memory Management  Bitwise AND Operator (&)  Bitwise Inclusive

    OR Operator (|)  Bitwise Exclusive OR Operator (^)  One’s Complement Operator (~)  Left Shift (<<) and Right Shift (>>) Operators  Logical Operators & Bitwise Assignment Operators