Q:

How do I get a bit from an integer value in C?

0

How do I get a bit from an integer value in C?

All Answers

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

To get the ith bit, perform Anding operation between the ith bit and 1 (1 << i) after that shift the result ith position in right using the right operation.

#include <stdio.h>
//Macro to Get bit from the given position
#define GET_BITS(data, pos) ((data & ( 1 << pos)) >> pos)
int main()
{
    unsigned char value = 16; //value in hex 00010000
    unsigned char position = 1;
    printf("%d\n", GET_BITS(value,position)); //print gets value from the 1th position
    position = 4;
    printf("%d\n", GET_BITS(value,position)); //print gets value from 3rd position
    return 0;
}

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

total answers (1)

Write the macros to set, clear, toggle and check t... >>
<< Swap two nibbles of a byte...