Review:   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 

We have used a shift operation to construct the pattern 000..00100..000 before !!

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

  • Recall how to construct the pattern:

                   position k
                       |
                       V
      pattern = 000..00100..000

  • We start with the following binary pattern:

      000....0....0001   (= 1 in decimal)
    

  • Then shift the pattern k positions to the left:

      000..00100..0000   (= 1 << k )
    

Algorithm 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(1 << k)
                ---------------- AND
             x = 0000...?...0000    (y AND 0 = 0 and ? AND 1 = ?)
    
       ISSET(x,k) (x & (1 << k)) != 0 

Note: C programmers often define ISSET as a macro:   #define ISSET(x,k) ((x & (1 << k)) != 0)

Application of ISSET(x,k)

ISSET(x,k) can be used to print a variable out in binary by testing each individual bit in the variable:

#include <stdio.h>
void printBits(int x)  // Function declaration
#define ISSET(x,k) ((x & (1 << k)) != 0)

int main( int argc, char* argv[] )
{
   int a = 7;

   printf("a = %d, in binary: ", a);
   printBits(a);
   printf("\n\n");
}

void printBits(int x)
{
   for (int i = 31; i >= 0; i--)
   {   Print x out in binary
       if ( x & (1 << i) /* i-th bit in x is 1 */ )           
          printf("1");
       else
          printf("0");
   }
} 

Application of ISSET(x,k)

ISSET(x,k) can be used to print a variable out in binary by testing each individual bit in the variable:

#include <stdio.h>
void printBits(int x)  // Function declaration
#define ISSET(x,k) ((x & (1 << k)) != 0)

int main( int argc, char* argv[] )
{
   int a = 7;

   printf("a = %d, in binary: ", a);
   printBits(a);
   printf("\n\n");
}

void printBits(int x)
{
   for (int k = 31; k >= 0; k--) // Go through all 32 bits in x       
   {   
       if ( x & (1 << i) /* i-th bit in x is 1 */ ) 
          printf("1");
       else
          printf("0");
   }
} 

Application of ISSET(x,k)

ISSET(x,k) can be used to print a variable out in binary by testing each individual bit in the variable:

#include <stdio.h>
void printBits(int x)  // Function declaration
#define ISSET(x,k) ((x & (1 << k)) != 0)

int main( int argc, char* argv[] )
{
   int a = 7;

   printf("a = %d, in binary: ", a);
   printBits(a);
   printf("\n\n");
}

void printBits(int x)
{
   for (int k = 31; k >= 0; k--) // Go through all 32 bits in x       
   {   
       if ( ISSET(x,k) ) 
          printf("1");  // Bit k is 1 ==> print "1"
       else
          printf("0");  // Bit k is 0 ==> print "0"
   }
} 

DEMO: demo/C/set1/print-binary.c