# Get bits from an integer

**URL:** <https://discourse.nodered.org/t/get-bits-from-an-integer/66277>\
**Category:** General\
**Created:** [10 August 2022 10:41 UTC](https://discourse.nodered.org/t/get-bits-from-an-integer/66277 "2022-08-10T10:41:50Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ganesh](https://avatars.discourse-cdn.com/v4/letter/g/f19dbf/32.png) [@Ganesh](https://discourse.nodered.org/u/Ganesh)\
**Post date:** [10 August 2022 10:41 UTC](https://discourse.nodered.org/t/get-bits-from-an-integer/66277/1 "2022-08-10T10:41:50Z")

</div>

Hi  
I have an string of number Ex = 01001010 and I want to split it to 0,1,0,0,1,0 I am able to get that but it ignoring the zero in the at zero index  
I have attached flow below

```auto
[{"id":"60b50f5c.3a867","type":"debug","z":"7e597862c5e169b5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":430,"y":1180,"wires":[]},{"id":"fcc3e30d00183024","type":"inject","z":"7e597862c5e169b5","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0101001","payloadType":"num","x":140,"y":1180,"wires":[["743b24f6.14e19c"]]},{"id":"743b24f6.14e19c","type":"function","z":"7e597862c5e169b5","name":"convert","func":"let digits = msg.payload.toString().split('');\nlet realDigits = digits.map(Number)\n\nfunction sendOut(digit){\n node.send({payload:digit})\n}\n\nrealDigits.forEach(sendOut)","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":280,"y":1180,"wires":[["60b50f5c.3a867"]]}]

```

Thank You

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [10 August 2022 10:47 UTC](https://discourse.nodered.org/t/get-bits-from-an-integer/66277/2 "2022-08-10T10:47:01Z")

</div>

You are not injecting "0101001" but the number 0101001 (which is 101001)  
 ![Untitled 1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/c/5c1666a4b0080c4775695621b0fa73e32de3a631.jpeg)

---

<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:** [10 August 2022 13:28 UTC](https://discourse.nodered.org/t/get-bits-from-an-integer/66277/3 "2022-08-10T13:28:24Z")

</div>

Are you sure that is what you want to do? If so then why?

---

<div class="post-metadata">

**Author:** ![Ganesh](https://avatars.discourse-cdn.com/v4/letter/g/f19dbf/32.png) [@Ganesh](https://discourse.nodered.org/u/Ganesh)\
**Post date:** [11 August 2022 10:55 UTC](https://discourse.nodered.org/t/get-bits-from-an-integer/66277/4 "2022-08-11T10:55:55Z")

</div>

Hello,  
I have several sensors, and each one of them is giving me binary values of 1 bit. These data are summed, and then they are delivered to Node-red as integer values. I want to display the state of these sensors in the NR Dashboard, thus I must first convert the integers to binary and then split the binary string in order to display the status of each sensor separately.

---

<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:** [11 August 2022 13:15 UTC](https://discourse.nodered.org/t/get-bits-from-an-integer/66277/5 "2022-08-11T13:15:59Z")

</div>

If you have it as an integer number then you don't need to convert to a string then extract the bits, you can extract the bits directly. However, before I help you to code that, please feed the data that node-red is receiving into a debug node and show us what you get, and tell us what bit values you want to get out.

---

<div class="post-metadata">

**Author:** ![Ganesh](https://avatars.discourse-cdn.com/v4/letter/g/f19dbf/32.png) [@Ganesh](https://discourse.nodered.org/u/Ganesh)\
**Post date:** [12 August 2022 06:32 UTC](https://discourse.nodered.org/t/get-bits-from-an-integer/66277/6 "2022-08-12T06:32:22Z")

</div>

I am feeding int value to bit unloader to convert to binary and then splitting it to 16 bit length in which I am using first 11 bit values to show the status of 11 induvial sensor

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/0/401c723a3069f545305af5f334109114b4625336.png)

I have attached my flow below

```auto
[{"id":"743b24f6.14e19c","type":"function","z":"7e597862c5e169b5","name":"B Split ","func":"let digits = msg.payload.toString().split('');\nlet realDigits = digits.map(Number)\n\nfunction sendOut(digit){\n node.send({payload:digit})\n}\n\nrealDigits.forEach(sendOut)","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":730,"y":1060,"wires":[["3f3350fd4d38b875"]]},{"id":"fc1b8caae028857f","type":"inject","z":"7e597862c5e169b5","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"9","payloadType":"num","x":350,"y":1060,"wires":[["2142ba57fa993ebd"]]},{"id":"3f3350fd4d38b875","type":"debug","z":"7e597862c5e169b5","name":"debug 9","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":860,"y":1060,"wires":[]},{"id":"7818b2e05b4e2c89","type":"debug","z":"7e597862c5e169b5","name":"debug 10","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":860,"y":1100,"wires":[]},{"id":"2142ba57fa993ebd","type":"bitunloader","z":"7e597862c5e169b5","name":"","mode":"string","prop":"payload","padding":"16","x":490,"y":1060,"wires":[["7818b2e05b4e2c89","743b24f6.14e19c"]]}]

```

---

<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:** [12 August 2022 06:52 UTC](https://discourse.nodered.org/t/get-bits-from-an-integer/66277/7 "2022-08-12T06:52:10Z")

</div>

With the buffer-parser node, you can get all values in 1 object (then you can simply use them like `msg.payload.status.bit0`) without looping / splitting etc.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/9/2981a06bb7feb693a9169774b8dbcc13f230c61e.png)

```auto
[{"id":"fc1b8caae028857f","type":"inject","z":"b851e68919625d02","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[9]","payloadType":"json","x":2210,"y":600,"wires":[["61d003e0d43d3def"]]},{"id":"7818b2e05b4e2c89","type":"debug","z":"b851e68919625d02","name":"debug 10","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2360,"y":660,"wires":[]},{"id":"61d003e0d43d3def","type":"buffer-parser","z":"b851e68919625d02","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"16bitbe","name":"status","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"return","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":2360,"y":600,"wires":[["7818b2e05b4e2c89"]]}]

```

---

<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:** [12 August 2022 06:56 UTC](https://discourse.nodered.org/t/get-bits-from-an-integer/66277/8 "2022-08-12T06:56:16Z")

</div>

If you want individual values, you can set them up and map them to named items from any bit you specify...

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

```auto
[{"id":"fc1b8caae028857f","type":"inject","z":"b851e68919625d02","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[9]","payloadType":"json","x":2210,"y":600,"wires":[["61d003e0d43d3def"]]},{"id":"7818b2e05b4e2c89","type":"debug","z":"b851e68919625d02","name":"debug 10","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2360,"y":660,"wires":[]},{"id":"61d003e0d43d3def","type":"buffer-parser","z":"b851e68919625d02","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"bool","name":"power","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"bool","name":"fault","offset":0,"length":1,"offsetbit":1,"scale":"1","mask":""},{"type":"bool","name":"running","offset":0,"length":1,"offsetbit":2,"scale":"1","mask":""},{"type":"bool","name":"stopped","offset":0,"length":1,"offsetbit":3,"scale":"1","mask":""},{"type":"bool","name":"overrun","offset":0,"length":1,"offsetbit":4,"scale":"1","mask":""},{"type":"bool","name":"underrun","offset":0,"length":1,"offsetbit":5,"scale":"1","mask":""},{"type":"bool","name":"torqueOver","offset":0,"length":1,"offsetbit":6,"scale":"1","mask":""},{"type":"bool","name":"softLimit","offset":0,"length":1,"offsetbit":7,"scale":"1","mask":""},{"type":"bool","name":"hardLimit","offset":0,"length":1,"offsetbit":8,"scale":"1","mask":""}],"swap1":"swap16","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"value","resultTypeType":"return","multipleResult":true,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":2360,"y":600,"wires":[["7818b2e05b4e2c89"]]}]

```

---

<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:** [12 August 2022 06:57 UTC](https://discourse.nodered.org/t/get-bits-from-an-integer/66277/9 "2022-08-12T06:57:45Z")

</div>

(Last one)

If you want well defined names (so you can use `msg.payload.running` etc) it can do that too..

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/c/ecaa1652f9bf9ba0556ce94455dc5c8a031a0699.png)

```auto
[{"id":"fc1b8caae028857f","type":"inject","z":"b851e68919625d02","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[9]","payloadType":"json","x":2210,"y":600,"wires":[["61d003e0d43d3def"]]},{"id":"7818b2e05b4e2c89","type":"debug","z":"b851e68919625d02","name":"debug 10","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2360,"y":660,"wires":[]},{"id":"61d003e0d43d3def","type":"buffer-parser","z":"b851e68919625d02","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"bool","name":"power","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"bool","name":"fault","offset":0,"length":1,"offsetbit":1,"scale":"1","mask":""},{"type":"bool","name":"running","offset":0,"length":1,"offsetbit":2,"scale":"1","mask":""},{"type":"bool","name":"stopped","offset":0,"length":1,"offsetbit":3,"scale":"1","mask":""},{"type":"bool","name":"overrun","offset":0,"length":1,"offsetbit":4,"scale":"1","mask":""},{"type":"bool","name":"underrun","offset":0,"length":1,"offsetbit":5,"scale":"1","mask":""},{"type":"bool","name":"torqueOver","offset":0,"length":1,"offsetbit":6,"scale":"1","mask":""},{"type":"bool","name":"softLimit","offset":0,"length":1,"offsetbit":7,"scale":"1","mask":""},{"type":"bool","name":"hardLimit","offset":0,"length":1,"offsetbit":8,"scale":"1","mask":""}],"swap1":"swap16","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"return","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":2360,"y":600,"wires":[["7818b2e05b4e2c89"]]}]

```

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [12 August 2022 07:03 UTC](https://discourse.nodered.org/t/get-bits-from-an-integer/66277/10 "2022-08-12T07:03:14Z")

</div>

Woooo - Steve, you have opened a door to a place I've never seen.  
I can see a number of situations where my IoT students could use the buffer-parser.

---

<div class="post-metadata">

**Author:** ![Ganesh](https://avatars.discourse-cdn.com/v4/letter/g/f19dbf/32.png) [@Ganesh](https://discourse.nodered.org/u/Ganesh)\
**Post date:** [12 August 2022 09:41 UTC](https://discourse.nodered.org/t/get-bits-from-an-integer/66277/11 "2022-08-12T09:41:40Z")

</div>

Hi  
Couple of Questions

1. Can I get the value as ON and OFF Instead of True or False.
2. what is the best method to reverse this process Getting integer from Binary Values

Thanks

---

<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:** [12 August 2022 10:11 UTC](https://discourse.nodered.org/t/get-bits-from-an-integer/66277/12 "2022-08-12T10:11:17Z")

</div>

> [@Ganesh](#):
>
> Can I get the value as ON and OFF

Well since node-red runs on NodeJS (AKA JavaScript) - you can do whatever you like...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/c/2c8f370092a9f20aaf69c5f94e092f474cf6fcb0.png)

```auto
[{"id":"fc1b8caae028857f","type":"inject","z":"b851e68919625d02","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[9]","payloadType":"json","x":2710,"y":300,"wires":[["61d003e0d43d3def"]]},{"id":"7818b2e05b4e2c89","type":"debug","z":"b851e68919625d02","name":"before","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":3030,"y":300,"wires":[]},{"id":"61d003e0d43d3def","type":"buffer-parser","z":"b851e68919625d02","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"bool","name":"power","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"bool","name":"fault","offset":0,"length":1,"offsetbit":1,"scale":"1","mask":""},{"type":"bool","name":"running","offset":0,"length":1,"offsetbit":2,"scale":"1","mask":""},{"type":"bool","name":"stopped","offset":0,"length":1,"offsetbit":3,"scale":"1","mask":""},{"type":"bool","name":"overrun","offset":0,"length":1,"offsetbit":4,"scale":"1","mask":""},{"type":"bool","name":"underrun","offset":0,"length":1,"offsetbit":5,"scale":"1","mask":""},{"type":"bool","name":"torqueOver","offset":0,"length":1,"offsetbit":6,"scale":"1","mask":""},{"type":"bool","name":"softLimit","offset":0,"length":1,"offsetbit":7,"scale":"1","mask":""},{"type":"bool","name":"hardLimit","offset":0,"length":1,"offsetbit":8,"scale":"1","mask":""}],"swap1":"swap16","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"return","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":2860,"y":300,"wires":[["7818b2e05b4e2c89","ff89ed8f2744be8f"]]},{"id":"ff89ed8f2744be8f","type":"function","z":"b851e68919625d02","name":"true/false => ON/OFF","func":"for (let prop in msg.payload) {\n if (msg.payload.hasOwnProperty(prop)) {\n msg.payload[prop] = msg.payload[prop] === true ? \"ON\" : \"OFF\"\n }\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2820,"y":360,"wires":[["4bf5226f875fee1b"]]},{"id":"4bf5226f875fee1b","type":"debug","z":"b851e68919625d02","name":"after","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":3030,"y":360,"wires":[]}]

```

> [@Ganesh](#):
>
> Getting integer from Binary Values

What is a "Binary Value" - Binary is simply a base2 representation of an integer.  
e.g. `0b1111 === 15 === 0xF === 017` - proof...  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/7/e7f0b56ef9205a862d3365fd1f936d56fbb9d8a2.png)

So what exactly do you mean? A string of "1010101010101"? Where is your value coming from and what does it look like in the debug window?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [15 August 2022 08:34 UTC](https://discourse.nodered.org/t/get-bits-from-an-integer/66277/13 "2022-08-15T08:34:25Z")

</div>

A post was split to a new topic: [Combine and convert into an integer value](https://discourse.nodered.org/t/combine-and-convert-into-an-integer-value/66434)

---

<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:** [29 August 2022 08:34 UTC](https://discourse.nodered.org/t/get-bits-from-an-integer/66277/14 "2022-08-29T08:34:39Z")

</div>

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