Answer to Question #260830 in C for Mr Z

Question #260830

2. Using the online compiler •

Enter the code

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;
}


You will be prompted to enter a value for the number of shifts. Initially enter the value of zero

Repeat the previous two steps with values of 1, 2, 3, 4, 5, 6, 7 and 8


1
Expert's answer
2021-11-06T14:08:11-0400
#include<stdio.h>
int main(int argc, char *argv[]) {
	int first_byte = 128;
	int second_byte = 1;
	int shift;
	printf("Enter the number of left/right shifts: ");
	scanf("%d", &shift);
	printf("\n\n");
	first_byte = first_byte >> shift;
	second_byte <<= shift;		
	printf("128 >> %d = %d\n", shift, first_byte);
	printf("1 << %d = %d\n", shift, second_byte);
	system("pause");
	return 0;
}

Output

0 128>>0=128

1<<0=1


1 128>>1=64

1<<1=2


2 128>>2=32

1<<2=4


3 128>>3=16

1<<3=8


4 128>>4=8

1<<4=16


5 128>>5=4

1<<5=32


6 128>>6=2

1<<6=64


7 128>>7=1

1<<7=128


8 128>>8=0

1<<8=2 56




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS