+ 24
{Tips for C++} I thought this will be helpful for beginners(like me). Please someone explain this in detail.
Look at this program: #include <iostream> using namespace std; int main() { cout << 3 << 5 << endl; cout << (3 << 5) << endl; return 0; } Output:> First line: 35 Second line: 96
8 Respuestas
+ 9
3<<5 is a binary offset to the left
3 in binary is 11 so 3<<5 is 1100000, which is 2^5+2^6 = 32+64 = 96
You can also do it to the right :
3>>1 : 3 is 11 so 3>>1 is 1 in binary so also 1 in decimal
+ 4
You mean command
numb1 << numb2
add numb2 zeros to binary form of numb1 and then transfer it to decimal?
+ 3
Can you please explain better?
3 is binary 011, 5 is binary 101. How do we acquire 1100000 from that?
+ 3
saving for later💾
+ 1
11 is shifted 5 times to the left , so 5 zeros are appended.
+ 1
It does not transfer it to decimal, decimal are represented as binary so it just does a lower level operation
+ 1
its not an addition or anything, it's a bit shift, think of 3 not as 0 11 but 0000 0011. the 5 is not an int to worry about a value for, its a value for the bitwise shift left operand (<<). move those bits that express three 5 shifts left bitwise
0000 0110
0000 1100
0001 1000
0011 0000
0110 0000
which is your answer in binary so that's how its 96. you can go the other way as well and it gets more interesting if you allow all bits in your shift with(>>>). try 3>>>2
0000 0011
1000 0001 = -126 lol
+ 1
correction i meant 3>>>1 my bad