1

Was going through documentation (pseudocode) of a particular device. At one point it had:

int b0 = charToInt(*something*)
int Data;
Data = (b0<<4);

What does <<4 mean?

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
gkamani2011
  • 225
  • 1
  • 4
  • 14
  • 2
    Google for `shift operator in c` – Eugen Rieck Oct 15 '13 at 21:56
  • 1
    @EugenRieck - while it may be obvious to you that this is a shift operator, the OP may not have known that so wouldn't have been able to google for it. Having said that, the first google hit I get for "<< operator c" is http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B – Digital Trauma Oct 15 '13 at 22:05
  • It does not look like pseudo code. It looks like C. :) – ryyker Oct 15 '13 at 22:06
  • If you really want a laugh, extend @DigitalTrauma 's very good observation to some of the things I might have searched for when I first started (if Google had existed back then), i.e. "<<" or "<< C" or "what is <<". Half of the returned links were blocked by my companies firewall. I certainly would not have thought to append the words _C operator_ to any of those searches :) – ryyker Oct 15 '13 at 22:10
  • My comment was meant to be **helpfull** not derogatory! I actually **ment** "You will get your answer if you google for 'shift operator in C'". English is not my first language, I apologize if I sounded rude. Again: I didn't mean to. – Eugen Rieck Oct 16 '13 at 10:43
  • @EugenRieck - It was not taken derogatorily at all! Actually helpful. I just had a little fun with it, and actually went out and tried some variations of search patterns that someone fairly new to C might type in, choosing what someone might ask, based on my personality as the standard. Results were pretty hilarious. I hope I did not offend you. – ryyker Oct 16 '13 at 17:26

6 Answers6

1

That means to do a left shift 4 times on the binary representation of the number.

The number 4 = 0000 0100 in binary.

shifting the bit pattern left four times gives us 0100 0000

4 << 0 = 0000 0100 (what we start with)
4 << 1 = 0000 1000 (8)
4 << 2 = 0001 0000 (16)
4 << 3 = 0010 0000 (32)
4 << 4 = 0100 0000 (64)

0100 0000 = 64

yamafontes
  • 5,552
  • 1
  • 18
  • 18
1

It is a left shift operator, and can be used in this way:
To generate powers of 2 use the following :

1 << 0 == 1
1 << 1 == 2
1 << 2 == 4
1 << 3 == 8
1 << 4 == 16

and so on.

ryyker
  • 22,849
  • 3
  • 43
  • 87
0

bit shift 4 places to the left, so if b0 was 'A' it would be

'A' = 65 = 0100 0001

bit shifted to the left 4 times is

0100 0001 0000

Note: Ignoring bytes wapping you may or may not have

cmd
  • 5,754
  • 16
  • 30
0

<< is a bit operation, if I remember correctly. What it does is take the bit value of b0 and shift it over four places. so if b0 = 00000101 b0 << 4 = 01010000

I don't use bitwise operators so I can't really tell you what they're useful for, but hope this helps!

joshpaulchan
  • 183
  • 1
  • 1
  • 7
0

As the other answers have mentioned, this does a bitshift 4 bits to the left. Bit-shifting can be used to quickly multiply or divide by powers of 2. http://en.wikipedia.org/wiki/Bitwise_shift#Bit_shifts

  • Each bitshift to the left effectively multiplies by 2 (watch for overflows, and signed integers).
  • Each bitshift to the right effectively divides by 2.

So in this case you are doing:

  • shifting 4 bits to the left, or
  • multiplying by 2 four times, or
  • multiplying by (2 to the power 4), or
  • multiplying by 16.

Why would you want to multiply by 16 or <<4? Well lots of reasons, but one possibility immediately springs to mind - converting a hexadecimal string representation of a number to the number itself. Convert a hexadecimal string to an integer efficiently in C?

Community
  • 1
  • 1
Digital Trauma
  • 15,475
  • 3
  • 51
  • 83
-2

The << operator is a bitwise shift. That particular one is a left shift.

Generally these are used in compression algorithms, practical usage in anything else doesn't really exist save very few explicit areas like image pixel setting.

Phtes
  • 11
  • 2