버그는 아니겠지만,
>> logical shift 를 32이상 하게되면 0을 기대했으나,
다시 돌아오는 버그(?)가 있다.
(uint로 했는데 그냥 int로 해도 결과는 똑같다.)
main()
{
unsigned int a =
12345;
int i;
for(i=0; i< 50; i++)
{
printf("%2d %u\n", i, a<<i);
}
}
결과
0 12345
1 24690
2 49380
3 98760
4 197520
5 395040
6 790080
7 1580160
8 3160320
9 6320640
10 12641280
11 25282560
12 50565120
13 101130240
14 202260480
15 404520960
16 809041920
17 1618083840
18 3236167680
19 2177368064
20 59768832
21 119537664
22 239075328
23 478150656
24 956301312
25 1912602624
26 3825205248
27 3355443200
28 2415919104
29 536870912
30 1073741824
31 2147483648
32 12345
33 24690
34 49380
35 98760
36 197520
37 395040
38 790080
39 1580160
40 3160320
41 6320640
42 12641280
43 25282560
44 50565120
45 101130240
46 202260480
47 404520960
48 809041920
49 1618083840
0 0x3039
1 0x6072
2 0xc0e4
3 0x181c8
4 0x30390
5 0x60720
6 0xc0e40
7 0x181c80
8 0x303900
9 0x607200
10 0xc0e400
11 0x181c800
12 0x3039000
13 0x6072000
14 0xc0e4000
15 0x181c8000
16 0x30390000
17 0x60720000
18 0xc0e40000
19 0x81c80000
20 0x3900000
21 0x7200000
22 0xe400000
23 0x1c800000
24 0x39000000
25 0x72000000
26 0xe4000000
27 0xc8000000
28 0x90000000
29 0x20000000
30 0x40000000
31 0x80000000
32 0x3039
33 0x6072
34 0xc0e4
35 0x181c8
36 0x30390
37 0x60720
38 0xc0e40
39 0x181c80
40 0x303900
41 0x607200
42 0xc0e400
43 0x181c800
44 0x3039000
45 0x6072000
46 0xc0e4000
47 0x181c8000
48 0x30390000
49 0x60720000
a << shift;
shift>=sizeof(a)*8 ? 0 : a << shift;
이렇게 쓰면 되겠다.
댓글