HELP - BIN to char* or String

Hello
I'm working with an ESP32 board that uses a shift register 74HC165.
In the firmware of this board there is a function that returns the digitalRead of the pins in binary:

static uint64_t in_data(void)
{
  uint64_t data = 0;
  in_latch_pulse();
  for(int i=0;i<48;i++)
  {
    data = data | ((uint64_t)digitalRead(IN_DATA_PIN))<<i;
    clock_pulse();
  }

  Serial.println(data, BIN);
  return data;
}

The output on the serial monitor is like this:

13:02:55.165 -> 111111111101111111111111111111111111111111111111
13:02:55.165 -> 111111111101111111111111111111111111111111111111
13:02:55.165 -> 111111111101111111111111111111111111111111111111

It works fine, however, I needed to extract from this BIN value the value of only one of the pins, for example, extract the value only from position 10 (which in the example above would be just 0).

I needed to save this information in BIN in a String or char* variable to be able to extract the positions I want, but I couldn't do that.

Would anyone have any alternative to help me with this issue?

Thanks in advance.

Hi,
try this line:

Serial.print(bitRead(data, 10)); // print bit

or
byte data1;

bitWrite(data1, 2, bitRead(data, 10));

No, you don't. You can use math:
(data & 0b000000000100000000000000000000000000000000000000ull) != 0
That expression will be 'true' (1) if the tenth bit from the left of your 48-bit value (38th from the right) is a 1 and false (0) if the bit is 0.

You can replace the long binary constant with (1ull << 38)

Thanks! It worked really well :slight_smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.