-5

Im having trouble understanding what << means in C. Im supposed to explain some code as a homework assignment, but it's kinda difficult to find an answer by googling.

It's related to programming a multi function shield connected to arduino (atmega328p chip)

Here's the line:

PORTB = ~(money << 2);

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • my teacher is such a good teacher, that he has not given us any books but presents hour long PP presentations expecting us to know everything – Flipper Flanné Sep 28 '19 at 11:18
  • Oh dear. Good luck. – Yunnosch Sep 28 '19 at 11:26
  • 1
    "Kind of difficult to find an answer by googling." Some things are like that; some bodies of knowledge defy the instant-gratification answer. Sometimes you have to read the whole book (at the risk, it's true, of learning more than just the one answer you're looking for today). – Steve Summit Sep 28 '19 at 11:26
  • Most languages have lots of operators, some more obscure than others. Most C references have a section devoted to all of them, though probably not in the introductory chapter 1. In [these online course notes](https://www.eskimo.com/~scs/cclass/cclass.html), `<<` and `>>` don't show up until [section 18.2.1](https://www.eskimo.com/~scs/cclass/int/sx4b.html). – Steve Summit Sep 28 '19 at 11:32
  • `<<` means left-shift in C. Google "bitwise-operators" to learn more. – Aykhan Hagverdili Sep 28 '19 at 12:28

1 Answers1

-1

From the C Standard (6.5.7 Bitwise shift operators)

4 The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are filled with zeros. If E1 has an unsigned type, the value of the result is E1 × 2E2, reduced modulo one more than the maximum value representable in the result type. If E1 has a signed type and nonnegative value, and E1 × 2E2 is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined.

5 The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type or if E1 has a signed type and a nonnegative value, the value of the result is the integral part of the quotient of E1 / 2E2. If E1 has a signed type and a negative value, the resulting value is implementation-defined.

In fact this operation

money << 2

is equivalent to

4 * money
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335