Skip to content

Instantly share code, notes, and snippets.

@thomasnield
Created July 18, 2022 01:36
Show Gist options
  • Save thomasnield/606e49f9cc173815acf6080b03165182 to your computer and use it in GitHub Desktop.
Save thomasnield/606e49f9cc173815acf6080b03165182 to your computer and use it in GitHub Desktop.
Bit Shifting and Byte Display
// C program to demonstrate the
// showbits() function
#include <stdio.h>
#define BV(bit) (1 << bit)
#define setBit(byte, bit) (byte |= BV(bit))
#define clearBit(byte, bit) (byte &= ~BV(bit))
#define toggleBit(byte, bit) (byte ^= BV(bit))
#define getBit(byte, bit) (byte >> bit) & BV(0)
void showByte (int value) {
int n;
for (n=8-1;n>=0;n--) {
printf("%d",(value >>n)&1);
}
}
// demonstrate displaying of byte and retrieving of value
int main()
{
int value = 0b0100100;
int n;
showByte(value);
printf("\n");
printf("%d", getBit(value,2));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment