Skip to content
Related Articles

Related Articles

Improve Article

showbits( ) Function in C with Examples

  • Last Updated : 08 Mar, 2021
Geek Week

Bitwise operators are operators (just like +, *, &&, etc.) that operate on ints and uints at the binary level. This means they look directly at the binary digits or bits of an integer. This all sounds scary, but in truth, bitwise operators are quite easy to use and also quite useful! It is important, though, that you have an understanding of binary numbers and hexadecimal numbers.

Bitwise Operators

Let’s take a look at the different bitwise operators:

  1. & (bitwise AND)
  2. | (bitwise OR)
  3. ~ (bitwise NOT)
  4. ^ (bitwise XOR)
  5. << (bitwise left shift)
  6. >> (bitwise right shift)
  7. >>> (bitwise unsigned right shift)
  8. &= (bitwise AND assignment)
  9. |= (bitwise OR assignment)
  10. ^= (bitwise XOR assignment)
  11. <<= (bitwise left shift and assignment)
  12. >>= (bitwise right shift and assignment)
  13. >>>= (bitwise unsigned right shift and assignment)

In this article, the focus is on the showbits() function. Let’s see how it is related to Bitwise Operators.

showbits( ) Function

This function mainly deals with bitwise operators concepts. Let’s have a look at the below C program to understand showbits() function.

C




// C program to demonstrate the
// showbits() function
#include <stdio.h>
void showbits (int n)
{
  int i, k, andmask;
  
  for (i = 15; i >= 0;i--)
  {
    andmask = 1 << i;
    k = n & andmask;
    k == 0 ? printf ("0") : printf ("1");
  }
  
// Driver code
int main() 
{
  showbits(5);    
  return 0;
}
Output



0000000000000101

Explanation

  1. All that is being done in this function is using an AND operator and a variable andmask. andmask variable is used to check the status of individual bits. If the bit is OFF we print a 0 otherwise we print a 1.
  2. The first time through the loop, the variable andmask will contain the value 1000000000000000, which is obtained by left-shifting 1, fifteen places.
  3. If the variable n’s most significant bit is 0, then k would contain a value 0, otherwise, it would contain a non-zero value.
  4. If k contains 0 then printf( ) will print out 0 otherwise it will print out 1.
  5. On the second go-around of the loop, the value of i is decremented, and hence the value of andmask changes, which will now be 100000000000000. This checks whether the next most significant bit is 1 or 0, and prints it out accordingly. The same operation is repeated for all bits in the number.

If the binary of decimal numbers is required, then basically all integers are actually in binary in computer. Its just that it is turned into a string that is the decimal representation of that value when it is printed using printf and “%d”. If result in some other base (e.g. base 2 or binary) is required, provide the proper printf format string if it exists (e.g. “%x” for hex) or just build that string and print it out.

Below is the code that can build the string representation of an integer in any base in [2,36].

C




// C program to implement the 
// above approach
#include <stdio.h>
#include <string.h>
  
char digits[] = 
     "01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  
void reverse(char* start, char* end)
{
  for(end--; start < end; start++, end--)
  {
    char t = *start;
    *start = *end;
    *end = t;
  }
}
  
int base_change(int n, 
                int base, char* buffer)
{
  int pos = 0;
    
  if (base > strlen(digits)) 
    return -1;
  while(n)    
  {
    buffer[pos] = digits[n % base];
    n /= base;
    pos++;
  }
    
  buffer[pos] = '\0';
  reverse(buffer, buffer + pos);
  return 0;
}
  
// Driver code
int main() 
{
  char buffer[32];
  int conv = base_change(1024, 
                         2, buffer);
  if (conv == 0) 
    printf("%s\n", buffer);
  return 0;
}
Output
10000000000

Explanation: The function base_change() displays the binary representation of any integer or character value. For now, it is sufficient to know that base_change() function displays the binary equivalent of the integer number.

Take a step-up from those “Hello World” programs. Learn to implement data structures like Heap, Stacks, Linked List and many more! Check out our Data Structures in C course to start learning today.



My Personal Notes arrow_drop_up
Recommended Articles
Page :