#include <stdio.h> int main(){ int x = 1 << 32; int y = 32; int z = 1 << y; printf("x:%d, z: %d ", x, z);
}
以上运行结果: x:0, z: 1
汇编代码如下:
1 .file "tmp.c" 2 .section .rodata 3 .LC0: 4 .string "x:%d, z: %d " 5 .text 6 .globl main 7 .type main, @function 8 main: 9 .LFB0: 10 .cfi_startproc 11 pushq %rbp 12 .cfi_def_cfa_offset 16 13 .cfi_offset 6, -16 14 movq %rsp, %rbp 15 .cfi_def_cfa_register 6 16 subq $16, %rsp 17 movl $0, -4(%rbp) ; ? 18 movl $32, -8(%rbp) ; y = 32; 19 movl -8(%rbp), %eax ; $32 => %eax 20 movl $1, %edx ; $1 => %edx 21 movl %eax, %ecx ; $32 => %ecx 22 sall %cl, %edx ; 1 << 32 23 movl %edx, %eax ; (1 << 32) => %eax 24 movl %eax, -12(%rbp) ; z 25 movl -12(%rbp), %edx ; z => %edx 26 movl -4(%rbp), %eax ; $0 => %eax 27 movl %eax, %esi 28 movl $.LC0, %edi 29 movl $0, %eax 30 call printf 31 leave 32 .cfi_def_cfa 7, 8 33 ret 34 .cfi_endproc 35 .LFE0: 36 .size main, .-main 37 .ident "GCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-16)" 38 .section .note.GNU-stack,"",@progbits
看汇编代码, 1 << 32 和 1 << y 明显是不一样的机制实现的, 虽然我也看不懂~~~
这个问题也是在Stack Overflow上看到的,原文地址https://stackoverflow.com/questions/3871650/gcc-left-shift-overflow