Macros for Bit Manipulation in C

Macros for Bit Manipulation in C/C++

We have already written an article on bit operation and already covered many tricky questions related to bit manipulation. In this blog post, we will only discuss how to write macros for bit manipulation. So let’s see some macros for bit manipulation.

If you are new and don’t know how to manipulate bits in C/C++ using the unary operator, you can see the below article.

Setting bit using macro:

We use the bitwise OR operator (|) to set a bit. x |= (1U<< pos); it will set nth bit .

//Macro to set nth-bit

/* 
Set single bit at pos to '1' by generating a mask
in the proper bit location and ORing (|) x with the mask. 
*/

#define SET_BIT(x, pos) (x |= (1U << pos))

 

Clearing bit using macro:

We use the bitwise AND operator (&) to clear a bit. x &= ~(1UL << pos); it will clear nth bit. You must invert the bit string with the bitwise NOT operator (~), then AND it.

//Macro to clear nth-bit

/* 
Set single bit at pos to '0' by generating a mask
in the proper bit location and Anding x with the mask. 
*/

#define CLEAR_BIT(x, pos) (x &= (~(1U<< pos)))

 

 

Toggling bit using macro (Macro to flip bit):

We use the bitwise XOR operator (^) to toggle a bit. x^= 1U << pos;  it will toggle nth bit .

//Macro to toggle nth-bit

/*
Set single bit at pos to '1' by generating a mask
in the proper bit location and ex-ORing x with the mask. 
*/

#define TOGGLE_BIT(x, pos) x ^= (1U<< pos)

 

 

Checking bit using macro:

We use the bitwise AND operator (&) to check a bit. x & (1UL << nth), it will check nth bit. To check the nth bit, shift the ‘1’ nth position toward the left and then “AND” it with the number.

//Macro to check nth-bit

/*
Set single bit at pos to '1' by generating a mask
in the proper bit location and Anding x with the mask.
It evaluates 1 if a bit is set otherwise 0.
*/

#define CHECK_BIT(x, pos) (x & (1UL << pos) )

 

Swap two nibbles of byte using macro:

A nibble consists of four bits. We use the << (left shift) and >> (right shift) operators to swap the nibble.

//Macro to swap nibbles

#define SWAP_NIBBLES(x) ((x & 0x0F)<<4 | (x & 0xF0)>>4)

 

Get bit value from integer using macro:

To get the nth bit, perform the Anding operation between the nth bit and 1 (1 << n) after that shift the result nth position to right using the right operation.

//Macro to Get bit from the given position
 
 
#define GET_BITS(x, pos) ((x & ( 1 << pos)) >> pos)

 

Swap the bytes in 32bit Integer using macro:

We use the << (left shift) and >> (right shift) operators to swap the byte.

//Macro to swap byte of 32-bit +ve integer variable


#define SWAP_BYTES(u32Value) ((u32Value & 0x000000FF) << 24)\
|((u32Value & 0x0000FF00) << 8) \
|((u32Value & 0x00FF0000) >> 8) \
|((u32Value & 0xFF000000) >> 24)

 

 

Swap all odd and even bits using macro:

We use the << (left shift), >> (right shift) and & (And) operators to swap the even and odd bits.

//macro to swap odd-even bits

#define SWAP_ODD_EVEN_BIT(x) ((x & 0xAAAAAAAA)>>1 | (x & 0x55555555)<<1);

 

 

Swap two numbers using a macro:

//macro to swap two numbers

#define SWAP(x, y) (x ^= y ^= x ^= y)

 

Get low and high bytes of an integer using a macro:

Note: Assumed size of int is 2 bytes.

//macro to get low and high bytes

#define LOWBYTE(v)   ((unsigned char) (x))


#define HIGHBYTE(v)  ((unsigned char) (((unsigned int) (x)) >> 8))

 

Recommended Post