blob: 8a54c567f80f249ccfb90af52b9b71a776dca271 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
/*
* What happens when the shift value is bigger than the amount of bits in the
* variable?
*
* Some machines will only use the lower log2 w bits of the shift amount when
* shifting a 2-bit falue.
*
* For example, shifting a 8-bit value by 10, would make the machine shift the
* value for only 2 (a k mod w).
*
* This is not a rule though. Let's test it.
*
* Looks like linux GCC on 64bit will always shift the exactly number, and crop
* only the exactly number of bits for the data type representation.
*
* For signed integers, if the shift is done right in the printf, gcc will use a
* convert it to a signed 32-bits for the conversion.
*
* Try change the x in printf on line 32, to use "a << 10" directly.
*/
#include <stdio.h>
int main(void)
{
/* 10101001 */
char x, a = 0xA9;
unsigned char y, b = 0xA9;
printf("Signed Original value: 0x%.2x\n", a);
x = a << 10;
printf("Signed left shift by 10: 0x%.2x\n\n", x);
printf("Unsigned Original value: 0x%.2x\n", b);
y = b << 10;
printf("Unsigned left shift by 10: 0x%.2x\n", y);
return 0;
}
|