2. Using the online compiler
• Enter the code from the file bit_shift_demo.c
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
byte_t first_byte = 128;
byte_t second_byte = 1;
int shift;
printf("Enter the number of left/right shifts: ");
scanf("%d", &shift);
printf("\n\n");
// Right-shift first_byte
first_byte = first_byte >> shift;
// Left-shift second byte
// This is the equivalent of second_byte = second_byte << shift;
second_byte <<= shift;
// Display the results
printf("128 >> %d = %d\n", shift, first_byte);
printf("1 << %d = %d\n", shift, second_byte);
system("pause");
return 0;
}
Run the program. You will be prompted to enter a value for the number of shifts. Initially enter the value of zero
• Add the output of this program to your logbook.
• Repeat the previous two steps with values of 1, 2, 3, 4, 5, 6, 7, and 8.
• Write a short reflective account of the code concentrating on its functionality and comparing the outputs against the source code.
Right shift (>>) Syntax. Description. This operator shifts the first operand the specified number of bits to the right. The code works efficiently and produces the right shift of numbers to the number of required steps.
Comments
Leave a comment