Bitwise Leftshift (<<) strange behavior

后端 未结 6 869
鱼传尺愫
鱼传尺愫 2021-01-15 13:41

gcc bitwise Leftshift (<<) strange behavior. Here is my code:

#include 
#include 

void foo(in         


        
6条回答
  •  说谎
    说谎 (楼主)
    2021-01-15 14:17

    Don't be surprised. you're dealing with a 32bit int, so when you do 1<<32, you've shifted that set bit right off the end of the int and zeroed out the whole thing.

    e.g. in binary:

        33222222 22211111 11111000 00000000
        10987654 32109876 54321098 76543210
     1: 00000000 00000000 00000000 00000001
    
       ^--- position #32
    

提交回复
热议问题