+ 8

What is output of this c++

cout<<(1<<1);

17th Jun 2017, 3:39 PM
#Happy@777
#Happy@777 - avatar
24 ответов
+ 13
<< x moves the bits to the left x places. 1 << 1 What is 1 in binary? = 01. Lets move that over to the left one place. = 10. 10 is 2 when converted to base 10. Lets do 2 << 3. 2 in binary: 000010 (There's actually lots of 0s before these numbers, I'm only writing a few to see what happens). Move the bits over 3 places we get: 010000 This is 16 when converted to base 10. ~Proof~ Binary: 0 1 0 0 0 0 Base10: 32 16 8 4 2 1 We have one 16, = 16. Meanwhile, if you did a >> x it would shift the bits of a to the right x times.
17th Jun 2017, 3:58 PM
Rrestoring faith
Rrestoring faith - avatar
+ 8
1=00000001 Now 1<<1 means shift bit to left by one position, we get 1<<1 = 00000010 = 2
17th Jun 2017, 3:56 PM
देवेंद्र महाजन (Devender)
देवेंद्र महाजन (Devender) - avatar
+ 7
2?
17th Jun 2017, 3:41 PM
khadeeja Shamna
khadeeja Shamna - avatar
+ 5
Here is the solution of the old one https://code.sololearn.com/c9Znd32f48sE/?ref=app
17th Jun 2017, 3:39 PM
Manual
Manual - avatar
+ 5
than how it is 16 in (2<<3) shamna ??
17th Jun 2017, 3:51 PM
#Happy@777
#Happy@777 - avatar
+ 5
2 = 00000010 2<<3 means shift bits to 3 position left 000010000 = 2 ^ 4 = 16
17th Jun 2017, 4:03 PM
देवेंद्र महाजन (Devender)
देवेंद्र महाजन (Devender) - avatar
+ 5
@NeutronStar Just keep in mind, the formula does not work when a bit reaches the 'edge'. Take for example: 2 >> 3 Solution: 0010 = 2. Move the 1's over to the right by 3: 0000, all 1's were lost. Answer: 0 Formula: = 2 / (2^3) = 2 / 8 = 1/4 0 =/= 1/4. So it may give unexpected results when this sort of thing occurs.
18th Jun 2017, 5:55 AM
Rrestoring faith
Rrestoring faith - avatar
+ 4
Try using (2<<3) The results are strange
17th Jun 2017, 3:45 PM
Manual
Manual - avatar
+ 4
I saw it before, but I do not know how to use it, to benefit my code.
17th Jun 2017, 3:48 PM
Manual
Manual - avatar
+ 4
Powers and logorithims thats useful. Python users keep saying C++, cannot do exponents. @NeutronStar Thank you for sharing!
18th Jun 2017, 12:48 PM
Manual
Manual - avatar
+ 4
@Restoring faith Very appropriate answers!! Keep going!
23rd Sep 2017, 4:31 AM
Abhishek Tandon
Abhishek Tandon - avatar
+ 4
@Restoring faith Very appropriate answers!! Keep going!
23rd Sep 2017, 4:31 AM
Abhishek Tandon
Abhishek Tandon - avatar
+ 3
for this ..??
17th Jun 2017, 3:40 PM
#Happy@777
#Happy@777 - avatar
17th Jun 2017, 3:42 PM
Manual
Manual - avatar
+ 3
how is 2 ..??
17th Jun 2017, 3:42 PM
#Happy@777
#Happy@777 - avatar
+ 3
it show 16 but how ????
17th Jun 2017, 3:47 PM
#Happy@777
#Happy@777 - avatar
+ 3
than how it is 16 in (2<<3) Mahender ??
17th Jun 2017, 3:58 PM
#Happy@777
#Happy@777 - avatar
+ 3
okay i got now ... thanks to all
17th Jun 2017, 4:01 PM
#Happy@777
#Happy@777 - avatar
+ 3
@Rrestoring faith, Thanks for pointing out the loss of data. But my answer is correct as far as "<<" and ">>" operators are implemented in programming languages like c/c++/java/python/ruby etc. Also if the number is not divisible by 2, then every right shift operation results in data loss. The results in actual binary arithmetic depends on 1. Number of bits in an integer on a target platform. The result can overflow for left shift (<<) and underflow for right shift (>>) 2. Representation of the number (affects right shift only) i.e. 2's complement or 1's complement 3. Type of shift operation (left or right) performed. The types are a. Arithmetic shift b. Logical shift Left Shift (Arithmetic or Logical) for positive numbers is same. The discarded bits are filled with zero and thus the final result is same as N * (2^x) In Right Arithmetic Shift (number represented as 2's complement) - MSB (most significant bit) bit is copied in the left and the bits are shifted right. The result is equivalent to N / (2^x) rounding towards negative infinity. If the binary number is represented as 1's complement then the result is same as N / (2^x) rounding towards zero In Right Logical shift, the MSB is filled with zero then the bits are shifted right. C/C++/Java/python/ruby etc. do Arithmetic shift by default. Shifting operations for negative numbers is undefined or implementation dependent (for c/c++ sure cant't say about other languages) Java also has operator for right logical shift, written as ">>>". There is no operator like "<<<". Thanks : )
18th Jun 2017, 11:49 AM
NeutronStar
NeutronStar - avatar
+ 2
it should show error ...
17th Jun 2017, 3:43 PM
#Happy@777
#Happy@777 - avatar