Programs


Title : Program to check Most Significant Bit of a number

#include <stdio.h>

#define BITS sizeof(int) * 8 // Total bits required to represent integer

int main()

{

   int num, msb;

   // Input number from user

   printf("Enter any number: ");

   scanf("%d", &num);

   // Move first bit of 1 to highest order

   msb = 1 << (BITS - 1);

   // Perform bitwise AND with msb and num

   if (num & msb)

       printf("MSB of %d is set (1).", num);

   else

       printf("MSB of %d is unset (0).", num);

   return 0;

}

OUTPUT:

Enter any number: 10

MSB of 10 is unset (0).

Related Topics

  • Program to get nth bit of a number

      Jun 25, 2021

  • program to check least significant bit of a number

      Jan 03, 2022

  • Program to reverse bits of an integer number.

      Jun 27, 2021

  • Program to check if all the bits of a given integer is one (1)

      Jun 27, 2021

  • Program to count number of bits set to 1 in an Integer

      Jun 27, 2021

  • Program to replace bit in an integer at a specified position from another integer

      Jun 27, 2021

  • Subscribe Now