#include #include #define BITS_IN_BYTE 8 // extract the nth bit from a value , assuming lsb is bit 0 // 76543210 // X // 00001101 // 00000011 // 00000001& //---------- // 00000001 uint8_t get_nth_bit(uint8_t value, int n) { uint8_t result = value >> n; result = result & 1U; return result; } // print the bottom how_many_bits bits of value void printBits(uint8_t value) { int i = BITS_IN_BYTE - 1; while (i >= 0){ printf("%u",get_nth_bit(value,i)); i--; } printf("\n"); } //Combining all code into one function and for an unsigned int. void printBits2(uint8_t a) { int j; j = BITS_IN_BYTE * (sizeof a); while (j > 0) { j = j - 1; printf("%d", (a >> j) & 1); } printf("\n"); } int main(void){ uint8_t x = 3; printf("%u\n",x); //Mult by 4 x = x << 2; printf("%u\n",x); //12 //Divide by 8 x = x >> 3; //1 printf("%u\n",x); //Print Powers of 2 printf("Powers of 2\n"); for(int n = 1; n < 7; n++){ printf("%u: %u\n",n, 1U << n ); //TODO } printf("\nPrinting numbers in binary\n"); for (int i = 0; i < 25; i++){ printBits(i); } //printBits(43); return 0; }