# How to use bit shift with i2cget

**URL:** <https://discourse.nodered.org/t/how-to-use-bit-shift-with-i2cget/70033>\
**Category:** General\
**Tags:** function-node\
**Created:** [3 November 2022 14:54 UTC](https://discourse.nodered.org/t/how-to-use-bit-shift-with-i2cget/70033 "2022-11-03T14:54:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aurgelme](https://avatars.discourse-cdn.com/v4/letter/a/74df32/32.png) [@Aurgelme](https://discourse.nodered.org/u/Aurgelme)\
**Post date:** [3 November 2022 14:54 UTC](https://discourse.nodered.org/t/how-to-use-bit-shift-with-i2cget/70033/1 "2022-11-03T14:54:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [3 November 2022 16:45 UTC](https://discourse.nodered.org/t/how-to-use-bit-shift-with-i2cget/70033/2 "2022-11-03T16:45:28Z")

</div>

I haven't used it but this might be helpful [node-red-contrib-bit (node) - Node-RED](https://flows.nodered.org/node/node-red-contrib-bit)

---

<div class="post-metadata">

**Author:** ![mudwalker](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@mudwalker](https://discourse.nodered.org/u/mudwalker)\
**Post date:** [3 November 2022 17:45 UTC](https://discourse.nodered.org/t/how-to-use-bit-shift-with-i2cget/70033/3 "2022-11-03T17:45:38Z")

</div>

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](https://stackoverflow.com/questions/1436438/how-do-you-set-clear-and-toggle-a-single-bit-in-javascript/1436448#1436448) to help me. You could use it to just check a bit and carry out the required action.

```auto
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

---

<div class="post-metadata">

**Author:** ![Aurgelme](https://avatars.discourse-cdn.com/v4/letter/a/74df32/32.png) [@Aurgelme](https://discourse.nodered.org/u/Aurgelme)\
**Post date:** [3 November 2022 20:53 UTC](https://discourse.nodered.org/t/how-to-use-bit-shift-with-i2cget/70033/4 "2022-11-03T20:53:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [17 November 2022 20:53 UTC](https://discourse.nodered.org/t/how-to-use-bit-shift-with-i2cget/70033/5 "2022-11-17T20:53:52Z")

</div>

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