Commonly used bit operations in computer programs

  • Commonly used bit manipulations in computer programs:

      • Set the bit at position k to 1 (without changing any other bit)

      • Clear/reset the bit at position k to 0 (without changing any other bit)

      • Flip the bit at position k (i.e.: 0 ⇒ 1 and 1 ⇒ 0) (without changing any other bit)

      • Test if the bit at position k is equal to 0 or 1,

  • We will first study a clumpsy way to:

      • Set the bit at position k to 1

  • Later:

    • Learn better ways to perform bit operations using shift operation

How to set the bit a position k to 1

  • Problem description:   set the bit at position k

                                         position k
                                             |
                                             V
     Given an arbitrary bit pattern:  1010...X...1101 
    
     Result after setting the bit
     at position k to 1:              1010...1...1101
      

  • Solution: initially, the bit at position k has the (arbitrary) value X (the value can be 0 or 1)

             a = 1010...X...1101 
       pattern = 0000...1...0000
                ---------------- OR
                 1010...1...1101
    

How to set the bit a position k to 1

  • Problem description:   set the bit at position k

                                         position k
                                             |
                                             V
     Given an arbitrary bit pattern:  1010...X...1101 
    
     Result after setting the bit
     at position k to 1:              1010...1...1101
      

  • Solution: construct the pattern 0000...1...0000 (it must only have a 1 at position k):

             a = 1010...X...1101 
       pattern = 0000...1...0000
                ---------------- OR
                 1010...1...1101
    

How to set the bit a position k to 1

  • Problem description:   set the bit at position k

                                         position k
                                             |
                                             V
     Given an arbitrary bit pattern:  1010...X...1101 
    
     Result after setting the bit
     at position k to 1:              1010...1...1101
      

  • Solution: apply an OR operation to set the bit at position k:

             a = 1010...X...1101 
       pattern = 0000...1...0000
                ---------------- OR
                 1010...1...1101    (X OR 1 = 1 and y OR 0 = y)
    

    The bit at position k is set because:          X OR 1 = 1
    The other bits are unchanged because:   y OR 0 = y

Problem

  • We must express the bit pattern:

       pattern = 0000...1...0000

  • The pattern 0000...1...0000 is a binary number


  • The numbers we use (=write) in computer proograms are expressed as decimal numbers

    • It is quite difficult to express (= write out) bit patterns as decimal numbers

    Example:

      Binary pattern       Decimal number
     -------------------------------------
         10000000               128
         01000000		     64
         00000100		      4
         ...
    

Example C program that set the bit at position k

  • Example: a C program that sets the bit #2 and then sets the bit #4:

    int main( int argc, char* argv[] )
    {
       char a = 0;		/* 0 = 00000000 */
    
       // Set bit 2
                            // Bit pos:   76543210
       a = a |  4;          //       4  = 00000100
                            // Result:    00000100 
    
       // Set bit 4
                            // Bit pos:   76543210
       a = a | 16;          // 16       = 00010000 
                            // Result:    00010100 
    }

  • Next topic:

    • Find a better way to write/express the bit pattern needed to perform bit operations

DEMO: demo/C/set1/bit-op2.c