+ 1

Tell reason of output 2. When this code runs:

#include <iostream> using namespace std; int main() { cout<<(1<<1); return 0; }

23rd Mar 2017, 9:54 AM
Gaurav Kaushik
Gaurav Kaushik - avatar
4 Antworten
+ 9
Tip: Left Shift by 1 = Multiplication by 2 Right Shift by 1 = Division by 2
23rd Mar 2017, 10:26 AM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 4
In binary. 00000001 << ( left shift ) one step the result will be 00000010 ( 2 )
23rd Mar 2017, 10:17 AM
K.C. Leung
K.C. Leung - avatar
+ 1
that implies <<<< =multiply 4
23rd Mar 2017, 11:03 AM
Gaurav Kaushik
Gaurav Kaushik - avatar
+ 1
not at all. let's the variable is declared as 8bit signed integer. ( byte ) the first bit is signed bit. (0)100 0000 = 64 64 << 1 = (1)000 0000 = -128 according to the C Language standard. The "byte" must be 8 bit. you may declare it as unsigned (0-255). for the type "int". it is depends on the OS and the CPU. Nowadays, it's usually 64bit length. For Java, the int must be 32bit signed. ( Java can not unsigned )
23rd Mar 2017, 12:58 PM
K.C. Leung
K.C. Leung - avatar