2

I am reading the code that involves some bitwise operations as shown below:

unsigned char data = 0;
unsigned char status = 0;

//DAQmx functions for reading data
DAQmxReadDigitalLines(taskHandleIn,1,10.0,DAQmx_Val_GroupByChannel,dataIn,8,&read,&bytesPerSamp,NULL);
DAQmxReadDigitalLines(taskHandleOut,1,10.0,DAQmx_Val_GroupByChannel,dataOutRead,8,&read,&bytesPerSamp,NULL);


for (int i = 0; i < 8; i++)
{
    if (dataOutRead[i] == 1)
        data = data | (0x01 << i);
    else
        data = data & ~(0x01 << i);
}

for (int i = 0; i < 4; i++)
{
    if (dataIn[i] == 1)
        status = status | (0x01 << (7 - i));
    else
        status = status & ~(0x01 << (7 - i));
}
ctrl = 0;

In the above codes, dataOutRead and dataIn are both uInt8 8-element arrays originally initialized to zero.

I don't quite understand what the code is actually doing? Anyone can walk me through these codes?

jwm
  • 4,832
  • 10
  • 46
  • 78
  • 1
    Suggested duplicate starts from the opposite end of the puzzle. This code does more complex interpretation of the data than simply setting bits. Voting to re-open. – Sergey Kalinichenko Feb 26 '18 at 18:08
  • 1
    Well, it's all there! ;-). Seriously: Look up the operators, take pen and paper and follow the instructions. (That's what I would do -- *sans* looking up the operators -- if I wanted to explain it to you, but you know, programmers are lazy...). If you are stuck somewhere specific or get an unexpected result, ask again. – Peter - Reinstate Monica Feb 26 '18 at 18:09
  • In C, a _bit field_ is a member of of struct like `unsigned x:5;`. This is more like "Data handling in bits". – chux - Reinstate Monica Feb 26 '18 at 18:13
  • There is no need for the bit-clearing with each `else` because the target values were initialised to `0`. – Weather Vane Feb 26 '18 at 18:15
  • 1
    "dataOutRead and dataIn are both uInt8 8-element arrays initialized to zero." then there is no need for either of the `for()` loops. Everything is zero. – chux - Reinstate Monica Feb 26 '18 at 18:16
  • @WeatherVane Oops, you are right. I noticed the switch from `dataOutRead` to `dataIn`, but missed the switch from `data` to `status`. Thank you! – Sergey Kalinichenko Feb 26 '18 at 18:20
  • jingweimo Tip for future: rather than _describe_ code, "dataOutRead and dataIn are both uInt8 8-element arrays", post the code to add clarity. – chux - Reinstate Monica Feb 26 '18 at 18:22
  • 1
    @chux: thanks for your reminding! – jwm Feb 26 '18 at 18:30

2 Answers2

2

Well the first loop is creating an unsigned char same as that of dataOutRead - Replicating whatever there is in dataOutRead to data. This one checks whether the ith bit is set/reset - and based on that it sets or resets in data.

Second loop does the same but with 4 least significant bits and copies whatever is there in most signigficant bits of status (Bit 7 to 4) from dataIn (but in reverse manner). To clarify further:-

7 6 5 4 3 2 1 0
x y z w w z y x

If in the second case 2 bit is set/reset then 5 bit of status is being set/reset.

user2736738
  • 30,591
  • 5
  • 42
  • 56
2

Key part of understanding this code is the conditional with a bitwise operation inside:

if(dataOutRead[i]==1) {
    data = data | (0x01 << i);
} else {
    data = data & ~(0x01 << i);
}

It uses bytes of dataOutRead as a sequence of ones and not ones (presumably, but not necessarily, zeros). This sequence is "masked" into bits of data starting with the least significant one:

  • When dataOutRead[i] is 1, the corresponding bit is set
  • When dataOutRead[i] is not 1, the corresponding bit is cleared. This step is unnecessary, because data is zeroed out before entering the loop.

This could be thought of as converting a "byte-encoded-binary" (one byte per bit) into its corresponding binary number.

The second loop does the same thing with reversed bits, processing only the lower four bits, and sticking them into the upper nibble of the data byte in reverse order.

It's hard to speculate on the purpose of this approach, but it could be useful in applications that use arrays of full-byte Booleans to control the state of some hardware register, e.g. in a microcontroller.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523