-4

I need help to understand what is happening in this declaration:

#define LDA(m)  (LDA_OP << 5 | ((m) & 0x001f))

Thank you

Joricam
  • 77
  • 3
  • 15

2 Answers2

0

y << x is a left shift of y by x. x & y is a bitwise and of x and y.

Community
  • 1
  • 1
Natecat
  • 2,175
  • 1
  • 17
  • 20
0

So, the left shift operator is like multiplying by 10 in base 10, but instead you multiply for 2 in base 2, for example:

In base 10

300 * 10 = 3000

In base 2:

0b0001 * 2 = 0b0010 = 0b0001 << 1

with a << b you "push" the number a, b places to the left.

and the or operator ( | ) you have to take two bits and if one or both of them are true (1) then the result is true.

For example:

0b0010 | 0b0001 = 0b0011

0b0010 | 0b0010 = 0b0010

If you have problems with this operators, just try to work the same numbers but in binary.

  • It still does not make sense to me, maybe because I also dont understand the OR bitwise operator? – Joricam May 10 '16 at 01:04
  • I know I have an hexadecimal value, but what am I doing to that value with the 5 | – Joricam May 10 '16 at 01:05
  • And then why do I "multiply it by 2" / shift left to LDA_OP ? – Joricam May 10 '16 at 01:05
  • Left shift is not the same as multiplying by 2 in base 2, because if the value goes over the number that can be contained by the number of bits in the variable, it will truncate the bits shifted over that – Natecat May 10 '16 at 20:08
  • Yes, you are right, but at least is a good base for starting to understand it. –  May 11 '16 at 00:41