Skip to content

Instantly share code, notes, and snippets.

@ti-nspire
Created February 20, 2019 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ti-nspire/3f9dd952432227e9155895d347ee57cf to your computer and use it in GitHub Desktop.
Save ti-nspire/3f9dd952432227e9155895d347ee57cf to your computer and use it in GitHub Desktop.
a function to print a one-byte value as a binary number.
#include <stdio.h>
// a function to print a one-byte value as a binary number.
void printByteAsBin(unsigned char oneByte){
unsigned char binStr[8];
unsigned char mask = 128; // 0b 1000 0000;
for(int i=0; i<8; i++){
binStr[i] = (oneByte & mask) ? '1' : '0' ;
mask >>= 1;
}
printf("%s\r\n", binStr);
}
int main(){
unsigned char oneByte = 0b11110000;
printByteAsBin(oneByte);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment