Created
July 18, 2022 01:36
-
-
Save thomasnield/606e49f9cc173815acf6080b03165182 to your computer and use it in GitHub Desktop.
Bit Shifting and Byte Display
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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