+ 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

3rd Mar 2017, 1:40 PM
Ram chandra Giri
Ram chandra Giri - avatar
8 odpowiedzi
+ 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
3rd Mar 2017, 2:13 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 4
You mean command numb1 << numb2 add numb2 zeros to binary form of numb1 and then transfer it to decimal?
4th Mar 2017, 8:43 PM
Victoria Tokareva
Victoria Tokareva - avatar
+ 3
Can you please explain better? 3 is binary 011, 5 is binary 101. How do we acquire 1100000 from that?
4th Mar 2017, 6:57 PM
Victoria Tokareva
Victoria Tokareva - avatar
+ 3
saving for later💾
27th Jun 2017, 11:07 PM
Manual
Manual - avatar
+ 1
11 is shifted 5 times to the left , so 5 zeros are appended.
4th Mar 2017, 7:39 PM
No One
No One - avatar
+ 1
It does not transfer it to decimal, decimal are represented as binary so it just does a lower level operation
4th Mar 2017, 10:14 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 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
7th Mar 2017, 1:11 AM
William La Flamme
William La Flamme - avatar
+ 1
correction i meant 3>>>1 my bad
7th Mar 2017, 1:12 AM
William La Flamme
William La Flamme - avatar