-2

I am going through example code and found this operation:

displayMap[x + (y/8)*LCD_WIDTH]|= 1 (shift by) shift; 

where

byte shift = y % 8;

I understand | operand and = but what are two of them together do.

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
semenoff
  • 63
  • 1
  • 1
  • 9
  • They do exactly that, bitwise or *and* assignment, all in one go. – Some programmer dude Nov 11 '16 at 15:43
  • 3
    `|=` is shorthand for doing an `OR` operation and assignment. For example,, `x |= 3` is equivalent to `x = x | 3`. You can also use other operators (`+, -, *, &`, etc) in this manner as well. – yano Nov 11 '16 at 15:43
  • The first hit in google answers your question. It's a tutorial so you might even learn some more C basics while you're there. – Carey Gregory Nov 11 '16 at 16:03
  • [What does “|=” mean? (pipe equal operator)](http://stackoverflow.com/q/14295469/995714) – phuclv Nov 11 '16 at 16:05
  • [C++ meaning |= and &=](http://stackoverflow.com/q/33304407/995714) – phuclv Nov 11 '16 at 16:14

1 Answers1

14

| performs a bitwise OR on the two operands it is passed.

For example,

byte b = 0x0A | 0x50;

If you look at the underlying bits for 0x0A and 0x50, they are 0b00001010 and 0b01010000 respectively. When combined with the OR operator the result in b is 0b01011010, or 0x5A in hexadecimal.

|= is analogous to operators like += and -= in that it will perform a bitwise OR on the two operands then store the result in the left operator.

byte b = 0x0A;
b |= 0x50;

// after this b = 0x5A
Govind Parmar
  • 20,656
  • 7
  • 53
  • 85