Deitel & Deitel, C How to Program 6th Edition, Exercise 10.10 (Right-Shifting Integers)

//  10.10 (Right-Shifting Integers)

#include <stdio.h>

void displayBits(int);
int main() {

int num = 20;
printf("Original Number:   ");
displayBits(num);
num <<= 4;
printf("Shifted by 4 bits: ");
displayBits(num);

printf("\n\n\n");
return 0;
}
void displayBits(int num) {
int mask = 1 << 31, i;
for (i = 1; i < 33; i++) {
printf("%d", mask & num ? 1 : 0);
num <<= 1; // num = num << 1
i % 8 ? 0 : printf(" ");
}
printf("\n");
}

OUTPUT

Original Number:   00000000 00000000 00000000 00010100
Shifted by 4 bits: 00000000 00000000 00000001 01000000



Press any key to continue . . .


Let me know in the comments if this page helped you somehow.

It is nice to know if my work helped someone, thanks :)

Comments

Popular posts from this blog

Deitel & Deitel, C How to Program 6th Edition, Exercise 6.23 (Turtle Graphics)

Deitel & Deitel, C How to Program 6th Edition, Exercise 6.22 (Total Sales)

Deitel & Deitel, C How to Program 6th Edition, Exercise 5.47 (Craps Game Modification)