When try to divide an integer by the power of two in two different ways, I get two different output. One way is to right shift it by k bits. Another way is to divide it by (1<<k).
#include <stdio.h>
int main(void) {
int y1 = 0x00061290;
int y2 = 0xFFFE1A32;
printf("(y1/(1<<k))=%x\n", (y1/(1<<5)));
printf("(y2/(1<<k))=%x\n", (y2/(1<<5)));
printf("(y1>>k))=%x\n", (y1>>5));
printf("(y2>>k))=%x\n", (y2>>5));
return 0;
}
Output:
(y1/(1<<k))=3094
(y2/(1<<k))=fffff0d2
(y1>>k))=3094
(y2>>k))=fffff0d1
When y1 is 0x00061290, the outputs of that integer are the same. When y2 is 0xFFFE1A32, the outputs of that integer are different.
I guess it is because y1 is positive and y2 is not. But I am not sure. Can someone tell me under what situation they are different and what situation they are the same? And explain the difference of two operations if possible. Thanks