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

//10.12 (Left - Shifting Integers)

#include <stdio.h>

int power2(int, int);
void displayBits(int);

int main() {
int num = 31;
displayBits(num);
num <<= 4;
displayBits(num);
num = power2(num, 4);
displayBits(num);



printf("\n\n\n");
return 0;
}

int power2(int number, int pow) {
int i;
for(i = 0; i < pow; i++)
number *= 2;
return number;
}

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

OUTPUT

00000000 00000000 00000000 00011111
00000000 00000000 00000001 11110000
00000000 00000000 00011111 00000000



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)