# Extraction int16 to bit

**URL:** <https://discourse.nodered.org/t/extraction-int16-to-bit/68364>\
**Category:** Industrial\
**Created:** [28 September 2022 20:56 UTC](https://discourse.nodered.org/t/extraction-int16-to-bit/68364 "2022-09-28T20:56:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nanar.vv](https://avatars.discourse-cdn.com/v4/letter/n/ea666f/32.png) [@nanar.vv](https://discourse.nodered.org/u/nanar.vv)\
**Post date:** [28 September 2022 20:56 UTC](https://discourse.nodered.org/t/extraction-int16-to-bit/68364/1 "2022-09-28T20:56:42Z")

</div>

Hello  
I'm looking for a function to extract the bits one by one from an int16 and and then work with conditionals.  
for expemple : if (M%12.1 & M%12.4){ var etat = "degivrage";} then { var etat = "marche";}

I had with a "for" function and var bit1 = {payload: bitraw[1]}; as soon as I work on the "bitx" variables I am confronted with "msg.payload: undefined /  
undefined"

On industrial automatons I will directly look for this variable. For example M%1260.6

Is there a function in java or pyton?

> Blockquote  
> //my existing code  
> // init bit  
> var bitbrut = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
> 
> for (i = 0; i \< 16; i++) {  
> bitbrut[i] = (msg.payload \>\> i) & 0x1;  
> }
> 
> var bit0 = { payload: bitbrut[0] };  
> var bit1 = { payload: bitbrut[1] };
> 
> if (bit0 = 1){  
> var etatcaisson = "arret";  
> }else {var etatcaisson = "marche";  
> }
> 
> return [etat, bit0, bit1];

> //Ma\_At %.0  
> //Demande\_froid %.1  
> //Demande\_ventilation %.2  
> //En\_Degivrage %.5  
> //En\_Egouttage %.6  
> //En\_Glacage %.7  
> //En\_Defaut %.8  
> //En\_Vidange %.9  
> //Fermeture\_volet %.10  
> //Ouverture\_volet %.11  
> //Phase\_froid %.12  
> //Rdm\_Vent\_degi %.14  
> //Rdm\_Vent\_froid %.13

I tried bit-simple-sixteen but it becomes too complicated with boolean blocks. I would like a function block that is easier to handle and more repeatable

Cordially

---

<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:** [29 September 2022 10:09 UTC](https://discourse.nodered.org/t/extraction-int16-to-bit/68364/2 "2022-09-29T10:09:31Z")

</div>

> [@nanar.vv](#):
>
> I tried bit-simple-sixteen but it becomes too complicated with boolean blocks. I would like a function block that is easier to handle and more repeatable

Or you can do it the low-code / no-code way using `node-red-contrib-buffer-parser`...

Setup...

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

Output...

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

---

<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:** [28 November 2022 10:09 UTC](https://discourse.nodered.org/t/extraction-int16-to-bit/68364/3 "2022-11-28T10:09:59Z")

</div>

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