+ 8
What is output of this c++
cout<<(1<<1);
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.
+ 8
1=00000001
Now 1<<1 means shift bit to left by one position, we get
1<<1 = 00000010 = 2
+ 7
2?
+ 5
Here is the solution
of the old one
https://code.sololearn.com/c9Znd32f48sE/?ref=app
+ 5
than how it is 16 in (2<<3) shamna ??
+ 5
2 = 00000010
2<<3 means shift bits to 3 position left
000010000 = 2 ^ 4 = 16
+ 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.
+ 4
Try using (2<<3)
The results are strange
+ 4
I saw it before, but
I do not know how to use it, to benefit my code.
+ 4
Powers and logorithims thats useful.
Python users keep saying C++, cannot do exponents.
@NeutronStar
Thank you for sharing!
+ 4
@Restoring faith Very appropriate answers!!
Keep going!
+ 4
@Restoring faith Very appropriate answers!!
Keep going!
+ 3
for this ..??
+ 3
Here is the current one.
https://code.sololearn.com/c9gRqO39ycaO/?ref=app
+ 3
how is 2 ..??
+ 3
it show 16 but how ????
+ 3
than how it is 16 in (2<<3) Mahender ??
+ 3
okay i got now ...
thanks to all
+ 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 : )
+ 2
it should show error ...