# Bit Strip from Int

**URL:** <https://discourse.nodered.org/t/bit-strip-from-int/67221>\
**Category:** General\
**Created:** [1 September 2022 16:23 UTC](https://discourse.nodered.org/t/bit-strip-from-int/67221 "2022-09-01T16:23:07Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![HansZnarf](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hansznarf/32/70215_2.png) [@HansZnarf](https://discourse.nodered.org/u/HansZnarf)\
**Post date:** [1 September 2022 16:23 UTC](https://discourse.nodered.org/t/bit-strip-from-int/67221/1 "2022-09-01T16:23:07Z")

</div>

This should be a simple application but we are struggling. The least significant bit is to the right.  
Bit 1 = Input 1 Register read = 1  
Bit 2 = Input 2 Register read = 2  
Bit 3 = Input 3 Register read = 4  
Bit 4 = Input 4 Register read = 8

Can make this work with = command when only one input is on however we cannot figure how to return a "0" when the output is greater or less than \>\< the value. Believe that would solve it. In many SCADA / HMI software packages there is a Bit Strip function that allows an extraction. Cannot find that in Node-Red. Thanks

---

<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:** [1 September 2022 16:57 UTC](https://discourse.nodered.org/t/bit-strip-from-int/67221/2 "2022-09-01T16:57:16Z")

</div>

If the value read is in msg.payload then something like

```auto
const b1 = (msg.payload & 1) 
const b2 = (msg.payload & 2) >> 1
const b3 = (msg.payload & 4) >> 2

```

etc.  
Or if you just want to test whether it is set then

```auto
if (msg.payload & 1) {}
if (msg.payload & 2) {}
if (msg.payload & 4) {}

```

---

<div class="post-metadata">

**Author:** ![HansZnarf](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hansznarf/32/70215_2.png) [@HansZnarf](https://discourse.nodered.org/u/HansZnarf)\
**Post date:** [1 September 2022 18:08 UTC](https://discourse.nodered.org/t/bit-strip-from-int/67221/3 "2022-09-01T18:08:45Z")

</div>

Colin: Many thanks. Would like a bool 1 as an output so each input can be presented in a dashboard as in:  
Input 1 to dashboard light 1  
Input 2 to dashboard light 2  
Input 3 to dashboard light 3  
Input 4 to dashboard light 4

They developers were not kind enough to put these in Input Registers so they need to be pulled out of an integer string. Kind of common way to do things in the industrial world.

---

<div class="post-metadata">

**Author:** ![HansZnarf](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hansznarf/32/70215_2.png) [@HansZnarf](https://discourse.nodered.org/u/HansZnarf)\
**Post date:** [1 September 2022 18:18 UTC](https://discourse.nodered.org/t/bit-strip-from-int/67221/4 "2022-09-01T18:18:02Z")

</div>

Here is how I would envision my code but do not know how to express this in Node-Red Json.  
Would make for functions block that harvest the number from the register. Seeing between 0 and 15 as expected with a Modbus reader.

Function 1  
If Input1 = 1 then msg.payload = 1  
If Input1 \<\>1 then mst.payload = 0

Function 2  
If Input1 = 2 then msg.payload = 1  
If Input1 \<\>2 then mst.payload = 0

Function 3  
If Input1 = 4 then msg.payload = 1  
If Input1 \<\>4 then mst.payload = 0

Function 4  
If Input1 = 8 then msg.payload = 1  
If Input1 \<\>8 then mst.payload = 0

Hope that makes sense. Believe the math works out.

Bit Unloader is giving me this:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/5/354048c396615c2b4ca0763f6319abda4232b3c7.png)

Do not know how to unpack it

Hans

---

<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:** [1 September 2022 19:04 UTC](https://discourse.nodered.org/t/bit-strip-from-int/67221/5 "2022-09-01T19:04:43Z")

</div>

That isn't JavaScript. What language are you working in and where does the function reside?

The code I posted gives the values as 0 or 1 in JavaScript.

---

<div class="post-metadata">

**Author:** ![HansZnarf](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hansznarf/32/70215_2.png) [@HansZnarf](https://discourse.nodered.org/u/HansZnarf)\
**Post date:** [1 September 2022 19:21 UTC](https://discourse.nodered.org/t/bit-strip-from-int/67221/6 "2022-09-01T19:21:31Z")

</div>

Understand it is not java script. More of what I have worked with before to show and example of logical data flow. Need a 1 output when it = 1, 2, 4 or 8 and a 0 when it does not. That should create and exclusive condition for each number.

---

<div class="post-metadata">

**Author:** ![HansZnarf](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hansznarf/32/70215_2.png) [@HansZnarf](https://discourse.nodered.org/u/HansZnarf)\
**Post date:** [1 September 2022 19:25 UTC](https://discourse.nodered.org/t/bit-strip-from-int/67221/7 "2022-09-01T19:25:59Z")

</div>

const b1 = (msg.payload & 1)  
const b2 = (msg.payload & 2) \>\> 1  
const b3 = (msg.payload & 4) \>\> 2

This Returns what was put into it on the debug Need pull the bits out individually

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [1 September 2022 19:32 UTC](https://discourse.nodered.org/t/bit-strip-from-int/67221/8 "2022-09-01T19:32:56Z")

</div>

depends what sort of input your led node can take - if the input can be defined to take `msg.payload.b0` etc for example then just do that... for each 'b'it.

If not you will need a set of change nodes (one per led) configured to move `msg.payload.b0` to `msg.payload` .... and same for '.b2', '.b3', etc etc

---

<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:** [1 September 2022 19:39 UTC](https://discourse.nodered.org/t/bit-strip-from-int/67221/9 "2022-09-01T19:39:05Z")

</div>

> [@HansZnarf](#):
>
> ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/5/354048c396615c2b4ca0763f6319abda4232b3c7.png)

If that is what you want then you can use

```auto
const b0 = (msg.payload & 1) 
const b1 = (msg.payload & 2) >> 1
const b2 = (msg.payload & 4) >> 2
const b3 = (msg.payload & 8) >> 3
msg.payload = {b0: b0, b1: b1, b2: b2, b3: b3}
return msg

```

---

<div class="post-metadata">

**Author:** ![HansZnarf](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hansznarf/32/70215_2.png) [@HansZnarf](https://discourse.nodered.org/u/HansZnarf)\
**Post date:** [1 September 2022 19:45 UTC](https://discourse.nodered.org/t/bit-strip-from-int/67221/10 "2022-09-01T19:45:59Z")

</div>

Thanks: That code returns this

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/5/350b0c1ead2942549089ef2e31c55fc911984ba4.png)

Still do not know how to pull these out as individual item for bool logic and dashboard.

More used to how PLCs each have a bool rung that can be used. How to we split these into four useable objects?

---

<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:** [1 September 2022 19:58 UTC](https://discourse.nodered.org/t/bit-strip-from-int/67221/11 "2022-09-01T19:58:26Z")

</div>

What _exactly_ do you want to do with it? What I have provided does have the four values available to do what you want with them. Unless you tell us exactly what that is then it is impossible to help.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [1 September 2022 20:14 UTC](https://discourse.nodered.org/t/bit-strip-from-int/67221/12 "2022-09-01T20:14:30Z")

</div>

Colin, I think the bit-unloader node already creates that object. And the ask is how to take those bits and feed into a series of leds on a dashboard. But which leds ?

> [@HansZnarf](#):
>
> Would like a bool 1 as an output so each input can be presented in a dashboard as in:  
> Input 1 to dashboard light 1  
> Input 2 to dashboard light 2  
> Input 3 to dashboard light 3

---

<div class="post-metadata">

**Author:** ![iiot2k](https://avatars.discourse-cdn.com/v4/letter/i/8baadc/32.png) [@iiot2k](https://discourse.nodered.org/u/iiot2k)\
**Post date:** [1 September 2022 20:21 UTC](https://discourse.nodered.org/t/bit-strip-from-int/67221/13 "2022-09-01T20:21:22Z")

</div>

The surprise with bit 31 in JavaScript 😊

---

<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:** [1 September 2022 20:26 UTC](https://discourse.nodered.org/t/bit-strip-from-int/67221/14 "2022-09-01T20:26:43Z")

</div>

> [@dceejay](#):
>
> I think the bit-unloader node already creates that object

Ah yes, looking back I see you are right.

@HansZnarf as Dave pointed out earlier, you can use a Change node to extract an individual value from the object. So a Change node configured like this would extract b1 as 1 or 0  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/b/3bc63cf618055af65feca6776a9b41cd84f7b7e6.png)

I recommend watching this playlist: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a lot in about 1 hour. A small investment for a lot of gain.

---

<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:** [1 September 2022 20:54 UTC](https://discourse.nodered.org/t/bit-strip-from-int/67221/15 "2022-09-01T20:54:07Z")

</div>

> [@iiot2k](#):
>
> surprise with bit 31 in JavaScript

Good point. In fact my code would be better using the unsigned shift operator

```auto
const b0 = (msg.payload & 1) 
const b1 = (msg.payload & 2) >>> 1
const b2 = (msg.payload & 4) >>> 2
const b3 = (msg.payload & 8) >>> 3
msg.payload = {b0: b0, b1: b1, b2: b2, b3: b3}
return msg

```

or

```auto
const b0 = msg.payload & 1
const b1 = (msg.payload >> 1) & 1
const b2 = (msg.payload >> 2) & 1
const b3 = (msg.payload >> 3) & 1
msg.payload = {b0: b0, b1: b1, b2: b2, b3: b3}
return msg

```

---

<div class="post-metadata">

**Author:** ![iiot2k](https://avatars.discourse-cdn.com/v4/letter/i/8baadc/32.png) [@iiot2k](https://discourse.nodered.org/u/iiot2k)\
**Post date:** [2 September 2022 03:17 UTC](https://discourse.nodered.org/t/bit-strip-from-int/67221/16 "2022-09-02T03:17:42Z")

</div>

JavaScript converts on all bit operation the value first to a 32bit **signed** integer.  
Bit 31 is then the sign.  
This is the surprise on bit operation with bit 31.

---

<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:** [2 September 2022 08:51 UTC](https://discourse.nodered.org/t/bit-strip-from-int/67221/17 "2022-09-02T08:51:08Z")

</div>

Yes, but I am not sure of the relevance of that here. Do you mean that the modified code I posted will not work?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [2 September 2022 09:04 UTC](https://discourse.nodered.org/t/bit-strip-from-int/67221/18 "2022-09-02T09:04:22Z")

</div>

Or you could use buffer-parser

> [@Understanding and parsing 16-bit words via Modbus](https://discourse.nodered.org/t/understanding-and-parsing-16-bit-words-via-modbus/67089/5):
>
> There is no need to use buffer maker as the data from the flex getter contains a buffer (use a debug node set to show complete message) Also, you can make a better output using the (arguably bad) syntax object=\>property e.g...

---

<div class="post-metadata">

**Author:** ![iiot2k](https://avatars.discourse-cdn.com/v4/letter/i/8baadc/32.png) [@iiot2k](https://discourse.nodered.org/u/iiot2k)\
**Post date:** [2 September 2022 10:50 UTC](https://discourse.nodered.org/t/bit-strip-from-int/67221/19 "2022-09-02T10:50:36Z")

</div>

Deleted by user.

---

<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:** [2 September 2022 11:34 UTC](https://discourse.nodered.org/t/bit-strip-from-int/67221/20 "2022-09-02T11:34:01Z")

</div>

My technique works ok for me.

```auto
[{"id":"97b2f6d0c1f1fa4d","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":4100,"wires":[["bccaca5778a53865"]]},{"id":"bccaca5778a53865","type":"function","z":"bdd7be38.d3b55","name":"function 1","func":"const value = Math.pow(2,31)\nmsg.payload = (value >>> 31) & 1\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":4100,"wires":[["c62e5740f8ffb42a"]]},{"id":"c62e5740f8ffb42a","type":"debug","z":"bdd7be38.d3b55","name":"debug 59","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":520,"y":4100,"wires":[]}]

```

Can you post an example where it doesn't work?

[Next page](https://discourse.nodered.org/t/bit-strip-from-int/67221.md?page=2)
