简体   繁体   中英

Shifting 1001 certain value gives a different result than expected

I am doing the following. I would like to shift 1001 to the left

int a = 0;
a = (1001 << 2);
std::cout << a; //Gives 4004

I was expecting it to be in binary 100100 which is 36. Why am I getting 0000111110100100 ?

1001 is a decimal constant. All integers literals are decimal unless prefixed accordingly. If you wish to obtain a binary constant, then prefix it with 0b (only available since C++14 though).

a = (0b1001 << 2);

Otherwise, hexadecimal is a decent alternative:

a = (0x9 << 2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM