Q:

Write the macros to set, clear, toggle and check the bit of a given integer

0

Write the macros to set, clear, toggle and check the bit of a given integer

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

  • #define SET_BIT(value, pos) value |= (1U<< pos)
  • #define CLEAR_BIT(value, pos) value &= ~(1U<< pos)
  • #define TOGGLE_BIT(value, pos) value ^= (1U<< pos)
  • #define CHECK_BIT_IS_SET_OR_NOT(value, pos) value & (1U<< pos)

Let see an example to set the bit using the above macro,

#include <stdio.h>
#define SET_BIT(value, pos) value |= (1U<< pos)
int main()
{
    //value
    unsigned int value =0;
    //bit position
    unsigned int pos = 0;
    printf("Enter the value\n");
    scanf("%d",&value);
    printf("Enter the position you want to Set\n");
    scanf("%d",&pos);
    SET_BIT(value,pos);
    printf("\n\n%dth Bit Set Now value will be = 0x%x\n",pos,value);
    return 0;
}

Output:

Enter the value

5

Enter the position you want to Set

1

1th Bit Set Now value will be = 0x7

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Write MACRO to swap the bytes in 32bit Integer Var... >>
<< How do I get a bit from an integer value in C?...