Skip to content

Instantly share code, notes, and snippets.

@RaghavSood
Created July 27, 2018 08:56
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 RaghavSood/7a6ed78d75b10d91d68c60c09ca3021a to your computer and use it in GitHub Desktop.
Save RaghavSood/7a6ed78d75b10d91d68c60c09ca3021a to your computer and use it in GitHub Desktop.
#include <stdio.h>
//assumes little endian
void printBits(size_t const size, void const * const ptr)
{
unsigned char *b = (unsigned char*) ptr;
unsigned char byte;
int i, j;
for (i=size-1;i>=0;i--)
{
for (j=7;j>=0;j--)
{
byte = (b[i] >> j) & 1;
printf("%u", byte);
}
}
puts("");
}
int main(void)
{
unsigned int number = 0b10001001000000000000000000111100;
printBits(sizeof(number), &number);
unsigned int unsignedpart = number << 11 >> 11;
printBits(sizeof(unsignedpart), &unsignedpart);
unsigned int signedpart = number << 1 >> 22;
printBits(sizeof(signedpart), &signedpart);
unsigned int sign = number >> 31;
printBits(sizeof(sign), &sign);
if (sign == 1) {
unsigned int fixedsign = signedpart ^ 0b10000000000000000000000000000000;
printBits(sizeof(fixedsign), &fixedsign);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment