Review:   How to set the bit at position k to 1

  • The SETBIT(x, k) operation: (sets the bit at position k in x)

                                   position k
                                       |
                                       V
     Before SETBIT(x,k):    x = 1010...?...1101 
     After  SETBIT(x,k):    x = 1010...1...1101  

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

             x = 1010...?...1101 
       pattern = 0000...1...0000
                ---------------- OR
                 1010...1...1101
    
    
    

     

Review:   How to set the bit at position k to 1

  • The SETBIT(x, k) operation: (sets the bit at position k in x)

                                   position k
                                       |
                                       V
     Before SETBIT(x,k):    x = 1010...?...1101 
     After  SETBIT(x,k):    x = 1010...1...1101  

  • Solution:   construct the pattern 000..00100..000 (it has a 1 at position k):

             x = 1010...?...1101 
       pattern = 000..00100..000
                ---------------- OR
                 1010...1...1101
    
    
    

     

Review:   How to set the bit at position k to 1

  • The SETBIT(x, k) operation: (sets the bit at position k in x)

                                   position k
                                       |
                                       V
     Before SETBIT(x,k):    x = 1010...?...1101 
     After  SETBIT(x,k):    x = 1010...1...1101  

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

             x = 1010...?...1101  
       pattern = 000..00100..000
                ---------------- OR
             x = 1010...1...1101    (y OR 0 = y and ? OR 1 = 1)
    
       SETBIT(x,k)x = x | pattern

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

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

How to clear the bit at position k to 1

  • The CLRBIT(x, k) operation: (clears the bit at position k in x)

                                   position k
                                       |
                                       V
     Before CLRBIT(x,k):    x = 1010...?...1101 
     After  CLRBIT(x,k):    x = 1010...0...1101  

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

             x = 1010...?...1101 
       pattern = 0000...1...0000
                ---------------- OR
                 1010...1...1101
    
    
    

     

How to clear the bit at position k to 1

  • The CLRBIT(x, k) operation: (clears the bit at position k in x)

                                   position k
                                       |
                                       V
     Before CLRBIT(x,k):    x = 1010...?...1101 
     After  CLRBIT(x,k):    x = 1010...0...1101  

  • Solution:   construct the pattern 111..11011..111 (it has a 0 at position k):

             x = 1010...?...1101 
       pattern = 111..11011..111
                ---------------- OR
                 1010...1...1101
    
    
    

     

How to clear the bit at position k to 1

  • The CLRBIT(x, k) operation: (clears the bit at position k in x)

                                   position k
                                       |
                                       V
     Before CLRBIT(x,k):    x = 1010...?...1101 
     After  CLRBIT(x,k):    x = 1010...0...1101  

  • Solution:   apply an AND operation to clear the bit at position k:

             x = 1010...?...1101  
       pattern = 111..11011..111
                ---------------- AND
             x = 1010...0...1101    (? AND 0 = 0 and y AND 1 = y)
    
       CLRBIT(x,k)x = x & pattern

    The bit at position k is cleared because:   ? AND 0 = 0
    The other bits are unchanged because:    y AND 1 = y

How to obtain the pattern used in the CLRBIT(x,k) operation

  • The pattern used to clear a bit at position k is:

                    position k
                        |
                        V
       pattern = 111..11011..111

  • It's easier to construct the pattern using the pattern 000..00100..000:

      111..11011..111~ 000..00100..000
                          ^
    		      |
               the bitwise NOT operation (flip each bit) !
    

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

How to flip the bit at position k

  • The FLIPBIT(x, k) operation: (flips the bit at position k in x)

                                    position k
                                        |
                                        V
     Before FLIPBIT(x,k):    x = 1010...?...1101 
     After  FLIPBIT(x,k):    x = 1010...¿...1101  

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

             x = 1010...?...1101 
       pattern = 0000...1...0000
                ---------------- OR
                 1010...1...1101
    
    
    

     

How to flip the bit at position k

  • The FLIPBIT(x, k) operation: (flips the bit at position k in x)

                                    position k
                                        |
                                        V
     Before FLIPBIT(x,k):    x = 1010...?...1101 
     After  FLIPBIT(x,k):    x = 1010...¿...1101  

  • Solution:   construct the pattern 000..00100..000 (it has a 1 at position k):

             x = 1010...?...1101 
       pattern = 000..00100..000
                ---------------- OR
                 1010...1...1101
    
    
    

     

How to flip the bit at position k

  • The FLIPBIT(x, k) operation: (flips the bit at position k in x)

                                    position k
                                        |
                                        V
     Before FLIPBIT(x,k):    x = 1010...?...1101 
     After  FLIPBIT(x,k):    x = 1010...¿...1101  

  • Solution:   apply an XOR operation to flip the bit at position k:

             x = 1010...?...1101  
       pattern = 000..00100..000
                ---------------- XOR
             x = 1010...¿...1101    (y XOR 0 = y and ? XOR 1 = ¿)
    
       FLIPBIT(x,k)x = x ^ pattern

    The bit at position k is flipped because: ? XOR 1 = ¿ (0 XOR 1 = 1 and 1 XOR 1 = 0)
    The other bits are unchanged because: y XOR 0 = y

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

How to test if the bit at position k is 0 or 1

  • The ISSET(x, k) operation: (returns true (!= 0) if bit k in x is 1, and returns false (== 0) otherwise)

              position k
                 |               if ? == 0:  ISSET(x,k) == 0
                 V               if ? == 1:  ISSET(x,k) != 0
      x = 1010...?...1101  

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

             x = 1010...?...1101 
       pattern = 0000...1...0000
                ---------------- OR
                 1010...1...1101
    
    
    

     

How to test if the bit at position k is 0 or 1

  • The ISSET(x, k) operation: (returns true (!= 0) if bit k in x is 1, and returns false (== 0) otherwise)

              position k
                 |               if ? == 0:  ISSET(x,k) == 0
                 V               if ? == 1:  ISSET(x,k) != 0
      x = 1010...?...1101  

  • Solution:   construct the pattern 000..00100..000 (it has a 1 at position k):

             x = 1010...?...1101 
       pattern = 000..00100..000
                ---------------- OR
                 1010...1...1101
    
    
    

     

How to test if the bit at position k is 0 or 1

  • The ISSET(x, k) operation: (returns true (!= 0) if bit k in x is 1, and returns false (== 0) otherwise)

              position k
                 |               if ? == 0:  ISSET(x,k) == 0
                 V               if ? == 1:  ISSET(x,k) != 0
      x = 1010...?...1101  

  • Solution:   apply an AND operation to test the bit at position k:

             x = 1010...?...1101  
       pattern = 000..00100..000
                ---------------- AND
             x = 0000...?...0000    (y AND 0 = 0 and ? AND 1 = ?)
    
       ISSET(x,k) (x & pattern) != 0 

    The bit at position k is retained because: ? AND 1 = ?
    All the other bits become 0 bits because: y AND 0 = 0

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