-2

I know that (10001010<<2) = 00101000

And that (10001010>>2) = 00100010

How to shift when I have only one bit like this (1<<5) and (1>>5)

Listy L
  • 52
  • 7
  • Possible duplicate of [Please let me know how does << and >> operator works in C?](https://stackoverflow.com/questions/5739664/please-let-me-know-how-does-and-operator-works-in-c) – roottraveller Aug 24 '17 at 12:24
  • `(10001010<<2) = 00101000` --> No. In C, the value to be shifted is at least 15 bits/signed or 16 bits/unsigned. – chux - Reinstate Monica Aug 24 '17 at 12:25

2 Answers2

0

The type of 1 in C is int, which is always larger than 1 bit.

Note that right-shifting a signed quantity is implementation-defined, but I guess most would give 0 since there's just a single 1 present and it's gone after the first bit shift.

unwind
  • 391,730
  • 64
  • 469
  • 606
0

First, if you do ( 010101 << 1 ) then it will consider "010101" as a decimal number and not a binary. The notation "0bxxx" tells the compiler that your number is binary ( 0b010101 ). For a single bit (your question), 1 decimal = 1 binary so you can use 1. However ( 1 >> anything ) should give you 0 all the time as you seem to know.

If you want to shift left, then this condition will return TRUE :

if( 8 == (1 << 3))

because (0b0001 << 3) = 0b1000 = 8