Created
July 27, 2018 08:56
-
-
Save RaghavSood/7a6ed78d75b10d91d68c60c09ca3021a to your computer and use it in GitHub Desktop.
This file contains 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
#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