How to use bit shift with i2cget

Hi, I've just started learning node-red, and don't know much about programming.
After reading many posts I have found that I may have to resort to bit-shifting to decode the hex value for an 8 channel relay board that I read in with i2cget.

I read address 0x3f and get 0xff (all off) to 0x00 (all on)
What I want to accomplish is to have 8 leds that indicate which relay is activated.

It works fine with switching the relays on and off, and I use a function node with "msg.payload = parseInt(msg.payload).toString(2);)" to display the binary value to easily see the relay state.

Can anyone point to info/code that can be used to learn more about this?
I am running raspberry pi with node-red 3.0.2

I haven't used it but this might be helpful node-red-contrib-bit (node) - Node-RED

I don't bitshift as such, but I do check individual bits of a 'word' to see if they are set or unset and change accordingly using a function node. Using a very useful post on Stack Exchange to help me. You could use it to just check a bit and carry out the required action.

let switchCH;
var mask = 1 << 8; // gets the 9th bit
var message = global.get('word to be masked')||0;
if ((message & mask) !== 0) switchCH = 1; else switchCH = 0;
msg.payload = switchCH;
//node.status({text:switchCH});
return msg;

HTH

Thank you both for your answers!
Colin's suggestion with "node-red-contrib-bit" worked perfectly

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