What is the 'bitwise or' operator doing?
lardom
Posts: 1,659
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.
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
So
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?
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.
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:
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: