int main(int argc, char *argv[])
{
unsigned int number;
unsigned int size;
unsigned int mask;
unsigned int pos;
printf ("Enter the number : ");
scanf ("%x", &number);
printf ("Enter position (0-31): ");
scanf ("%d", &pos);
printf ("Enter mask size (1-%d): ", 32-pos);
scanf ("%d", &size);
printf ("Number : ");
print_bits(number);
if(size == 32) {
mask = 0xFFFFFFFF;
} else {
mask = (1 << (size + 1)) - 1;
}
number = (number & (mask << pos)) >> pos;
printf ("\nBits : ");
print_bits(number);
return 0;
}
Output
Enter the number : 55AA55AA55
Enter position (0-31): 3
Enter mask size (1-29): 10
Number : 10101010010101011010101001010101
Bits : 00000000000000000000010101001010
Number : 10101010010101011010101001010101
Bits : ==>10101001010<==
About our authors: Team EQA
You have viewed 1 page out of 252. Your C learning is 0.00% complete. Login to check your learning progress.
‹
#
›