Shop OBEX P1 Docs P2 Docs Learn Events
What is the 'bitwise or' operator doing? — Parallax Forums

What is the 'bitwise or' operator doing?

lardomlardom Posts: 1,659
edited 2015-11-09 22:38 in Propeller 1
This code snippet is from "SPI Spin" written by Beau Schwabe. It's in his "SHIFTIN" method.
In this line; value := (Value << 1) | ina[Dpin] I'd like to know the purpose of "|"
"(Value << 1)" is an address. "PostClock" is a method. "Dpin" refers to data pin.


if Mode == 0                'MSBPRE
       repeat Bits
         value := (Value << 1) | ina[Dpin]
         PostClock(Cpin)

Comments

  • JasonDorieJasonDorie Posts: 1,930
    edited 2015-11-09 22:46
    Bitwise OR sets any ON bits in the result to any ON bits in either of the inputs.

    So
        %0011
    OR  %0101
    ==  %0111
    

    In this case, because he shifts "Value" up by one bit position first, and then does the OR operation, it has the effect of appending the state of the DPIN pin to the end of "value". The ina[DPIN] statement would be getting only a single bit value. That value is OR'd in to the lowest bit position of "Value", which has been cleared to zero by left-shifting the contents of Value up by one bit position.

    The clock pin is toggled to cause a device to update a pin, and the bits from that pin are read in and concatenated together into a numeric value.

    Does that make sense?
  • Thanks Jason. (I had to Google "concatenated"). This also means I'm going to have to learn to read hex.
    I'm waiting for a pair of nrf24L01 transceiver modules. The web is full of Arduino code so I'll have to learn how to operate the chips from datasheets.
  • Hex is relatively easy, but it takes some time.

    Quick primer:

    In decimal (base 10) each digit can be one of 10 values, from 0 to 9.
    In binary (base 2) each digit can be one of 2 values, from 0 to 1.
    In hex, (base 16) each digit can be one of 16 values, from 0 to 9, then A to F, which represent 10 to 15, but as a single "digit".

    Numbers are assembled digit by digit, like they are in decimal.

    The number 111, in each of the three bases, is assembled like this:
    
    Decimal:
        1 x 10^2  = 100  (hundreds place)
      + 1 x 10^1  = 10   (tens place)
      + 1 x 10^0  = 1    (ones place)
    = 111
    
    Binary:
        1 x 2^2 = 4  (fours place)
      + 1 x 2^1 = 2  (twos place)
      + 1 x 2^0 = 1  (ones place)
    = 8 (in decimal)
    
    Hex:
        1 x 16^2 = 256  (256's place)
      + 1 x 16^1 = 16   (16's place)
      + 1 x 16^0 = 1    (ones place)
    = 273 (in decimal)
    
    

    I used a number made of only 1's and 0's so I could include binary, but it works the same for other digits. Each digit moves up in "place". In decimal you have the 1's place (10 to the power of 0), then the ten's place (10 to the power of 1), then the hundreds place (10 to the power of 2) and so on.

    In hex, it's the 1's place (16 to the power of 0), then the 16's place (16 to the power of 1), and the 256's place (16 to the power of 2), and so on. Every time you move left one digit, the significance of that digit goes up by a multiple of the base value. (base 16 is hex, base 10 is decimal, and base 2 is binary). So in decimal you have 1's, 10's , 100's, 1000's (each one multiplying the one before by 10). In hex, it's 1's, 16's, 256's, 4096's, and so on, each multiplying the weight of the previous digit by 16.

    As another example:
    Hex 4A3 is four (256's) plus ten (16's) plus three (1's):
    
    4A3 =   4 x (16^2)  + A x (16^1)  + 3 x (16^0)
    4A3 =   4 x (256)  + (10) x (16)  + 3 x (1)     (because hex A is 10 in decimal)
    4A3 =   1024  + 160  +  3
    4A3 =   1187
    

Sign In or Register to comment.