Conditional check for “i == (2^8)” fails when i is 512?

后端 未结 6 1740
醉话见心
醉话见心 2021-01-04 00:41

Here is a small Program to print powers of 2 till 8. But it is not quitting after 8. Please explain the reason.

#include 
#include 

        
6条回答
  •  没有蜡笔的小新
    2021-01-04 01:26

    2^8 is 2 XOR 8, ie. 10, not 256. hence your loop doesn't stop. You probably want to check against either (1<<8) or 256 specifically.

提交回复
热议问题