# Bitwise syntax error ("error in JavaScript code")

**URL:** <https://discourse.nodered.org/t/bitwise-syntax-error-error-in-javascript-code/68466>\
**Category:** General\
**Created:** [1 October 2022 18:36 UTC](https://discourse.nodered.org/t/bitwise-syntax-error-error-in-javascript-code/68466 "2022-10-01T18:36:39Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![walberjunior](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/walberjunior/32/68528_2.png) [@walberjunior](https://discourse.nodered.org/u/walberjunior)\
**Post date:** [1 October 2022 18:36 UTC](https://discourse.nodered.org/t/bitwise-syntax-error-error-in-javascript-code/68466/1 "2022-10-01T18:36:39Z")

</div>

Hello. New to Node Red and Javascript.

I have the code below in a function, but even though it works as I expect, the function gets "error in JavaScript code".

```auto
msg.zone = ((msg.payload[1 + 1] >> 2) & (0x01 == 0x01));

```

How can I get rid of this "error in JavaScript code"?

From [https://beautifytools.com/](https://beautifytools.com/) I get this:  
Unexpected use of '\>\>'  
Unexpected use of '&'

I think I have to use something like this but I'm not sure

```auto
/*jslint bitwise: true */

```

Thanks

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [1 October 2022 18:40 UTC](https://discourse.nodered.org/t/bitwise-syntax-error-error-in-javascript-code/68466/2 "2022-10-01T18:40:55Z")

</div>

> [@walberjunior](#):
>
> `(0x01 == 0x01)`

What is this meant to do? It will always compile to `true` so seems a bit pointless?

> [@walberjunior](#):
>
> `msg.payload[1 + 1]`

This is also a bit pointless: `msg.payload[2]` is the same and assumes that the payload is an array - is it? Please share example content of `msg.payload`.

Since bitwise AND turns both operands to 32bit integers, `(0x01 == 0x01)` becomes TRUE which becomes `00000000000000000000000000000001` - you might as well have simply put in the number 1.

---

<div class="post-metadata">

**Author:** ![walberjunior](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/walberjunior/32/68528_2.png) [@walberjunior](https://discourse.nodered.org/u/walberjunior)\
**Post date:** [1 October 2022 18:58 UTC](https://discourse.nodered.org/t/bitwise-syntax-error-error-in-javascript-code/68466/3 "2022-10-01T18:58:02Z")

</div>

As you can see I'm not very good with programming.

Long story short, I took a code in golang and passed it to EspHome, but now I use it in Node Red 😄

> [@TotallyInformation](#):
>
> This is also a bit pointless: `msg.payload[2]` is the same and assumes that the payload is an array - is it? Please share example content of `msg.payload`.

I use 1 + 1 as "offset", because there are several functions and each of them with several lines

 ![node](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/4/f4645236b834949625a419f2b41832ce22001f64.jpeg)

```auto
msg.zone = ((msg.payload[1 + 1] >> 0) & (0x01 == 0x01))
msg.violated = ((msg.payload[1 + 8 + 1] >> 0) & (0x01 == 0x01))
msg.anulated = ((msg.payload[1 + 16 + 1] >> 0) & (0x01 == 0x01))
msg.tamper = ((msg.payload[1 + 43 + 1] >> 0) & (0x01 == 0x01))
msg.shortcircuit = ((msg.payload[1 + 44 + 1] >> 0) & (0x01 == 0x01))
msg.lowbattery = ((msg.payload[1 + 46 + 1] >> 0) & (0x01 == 0x01))
return msg;

```

> [@TotallyInformation](#):
>
> What is this meant to do? It will always compile to `true` so seems a bit pointless?

Returns true or false for multiple binary sensors.

> [@TotallyInformation](#):
>
> Please share example content of `msg.payload`.

````auto
37 E9 00 00 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 41 52 01 00 03 0C 0F 38 01 0A 16 00 00 00 00 FF 0F 00 00 00 00 00 00 00 00 00 00 00 00 00 E2```
````

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [1 October 2022 19:05 UTC](https://discourse.nodered.org/t/bitwise-syntax-error-error-in-javascript-code/68466/4 "2022-10-01T19:05:40Z")

</div>

Is the msg.payload actually an array? That looks more like a buffer. Can you output the msg to a debug node and show what the debug panel thinks it is?

The logic appears to be checking whether the input number is odd or even?

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

But a lot rides on the actual data type of the payload.

> [@walberjunior](#):
>
> Returns true or false for multiple binary sensors.

No, it really doesn't. Not unless you are going to replace one of those values with a variable instead. `(0x01 == 0x01)` always returns true at the moment. Also, a value of true in that part of the equation is meaningless because it gets converted to a 32bit integer with the value 1.

---

<div class="post-metadata">

**Author:** ![walberjunior](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/walberjunior/32/68528_2.png) [@walberjunior](https://discourse.nodered.org/u/walberjunior)\
**Post date:** [1 October 2022 19:44 UTC](https://discourse.nodered.org/t/bitwise-syntax-error-error-in-javascript-code/68466/5 "2022-10-01T19:44:52Z")

</div>

> [@TotallyInformation](#):
>
> That looks more like a buffer.

I forgot to reply, yes it's a buffer

 ![node2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/9/e9ef60bdf7a19d200473e101ee16700c0179de18.jpeg)

> [@TotallyInformation](#):
>
> The logic appears to be checking whether the input number is odd or even?

Forget the E9(I don't use it),, let's focus on the 04.

I need to check if third bit of buffer[4] is 1 or 0

`msg.zone = ((msg.payload[3 + 1] >> 2) & (0x01 == 0x01))`

It's working this way, it just returns the message "The workspace contains some nodes that are not properly configured:" when I deploy and the node gets this error:

![node3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/8/383c58662e8f8ba2b6759be7cad0d15093238c79.jpeg)

There's probably a better way to do this and get these errors out, but this was the best I could get.

The golang code is something like this:

```auto
(b[4]>>2&0x01 == 0x01)

```

---

<div class="post-metadata">

**Author:** ![walberjunior](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/walberjunior/32/68528_2.png) [@walberjunior](https://discourse.nodered.org/u/walberjunior)\
**Post date:** [1 October 2022 19:58 UTC](https://discourse.nodered.org/t/bitwise-syntax-error-error-in-javascript-code/68466/6 "2022-10-01T19:58:09Z")

</div>

> [@TotallyInformation](#):
>
> No, it really doesn't. Not unless you are going to replace one of those values with a variable instead. `(0x01 == 0x01)` always returns true at the moment. Also, a value of true in that part of the equation is meaningless because it gets converted to a 32bit integer with the value 1.

Returns 1 or 0 for sensor and for attributes, sensor is interpreted as on or off, true or false:

 ![node4](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/a/3a66b211c12bb7600a752152006683f681dbf696.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:** [2 October 2022 07:23 UTC](https://discourse.nodered.org/t/bitwise-syntax-error-error-in-javascript-code/68466/7 "2022-10-02T07:23:13Z")

</div>

> [@walberjunior](#):
>
> Returns 1 or 0 for sensor and for attributes, sensor is interpreted as on or off, true or false:

I don't understand what you mean. The code `(0x01 == 0x01)` will always return the value `true`, since obviously 0x01 is always equal to 0x01.

---

<div class="post-metadata">

**Author:** ![M-a-r-t-i-n](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/m-a-r-t-i-n/32/52391_2.png) [@M-a-r-t-i-n](https://discourse.nodered.org/u/M-a-r-t-i-n)\
**Post date:** [2 October 2022 07:34 UTC](https://discourse.nodered.org/t/bitwise-syntax-error-error-in-javascript-code/68466/8 "2022-10-02T07:34:26Z")

</div>

> [@walberjunior](#):
>
> `(b[4]>>2&0x01 == 0x01`

I think that something was lost in the translation from golang to JavaScript (by adding brackets in the wrong place)

In the golang it is shifting the value, ANDing it to mask the lowest bit off and then doing a test with 1.

The JavaScript presented earlier is doing it in the wrong order. It seems to be testing 1 against 1 and then ANDing it with the rest.

---

<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 October 2022 07:44 UTC](https://discourse.nodered.org/t/bitwise-syntax-error-error-in-javascript-code/68466/9 "2022-10-02T07:44:00Z")

</div>

> [@walberjunior](#):
>
> The golang code is something like this:
> 
> ```auto
> (b[4]>>2&0x01 == 0x01)
> 
> ```

I think exactly that code would work in javascript, at most you might need to add some parentheses  
`((b[4]>>2)&0x01 == 0x01)`  
I would have to test it to be sure.

---

<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 October 2022 08:25 UTC](https://discourse.nodered.org/t/bitwise-syntax-error-error-in-javascript-code/68466/10 "2022-10-02T08:25:05Z")

</div>

> [@M-a-r-t-i-n](#):
>
> The JavaScript presented earlier is doing it in the wrong order. It seems to be testing 1 against 1 and then ANDing it with the rest.

I suspect it is doing bitwise `&` followed by equality `==` then shift `>>`  
See here: [Operator precedence - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#table)

You can memorise operator precedence (or refer to docs) OR use brackets 🙂

---

<div class="post-metadata">

**Author:** ![M-a-r-t-i-n](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/m-a-r-t-i-n/32/52391_2.png) [@M-a-r-t-i-n](https://discourse.nodered.org/u/M-a-r-t-i-n)\
**Post date:** [2 October 2022 08:48 UTC](https://discourse.nodered.org/t/bitwise-syntax-error-error-in-javascript-code/68466/11 "2022-10-02T08:48:42Z")

</div>

> [@Steve-Mcl](#):
>
> I suspect it is doing bitwise `&` followed by equality `==` then shift `>>`  
> See here: [Operator precedence - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#table)

Hi Steve, I was referring to this JavaScript code which was given above. As I said the brackets have been put in the wrong place, and overrides the precedence used by the golang code it is trying to emulate.

msg.zone = ((msg.payload[1 + 1] \>\> 2) & (0x01 == 0x01));

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [2 October 2022 10:56 UTC](https://discourse.nodered.org/t/bitwise-syntax-error-error-in-javascript-code/68466/12 "2022-10-02T10:56:29Z")

</div>

In that case, perhaps your code should read:

```auto
msg.zone = ( (msg.payload[1 + 1] >> 2) & 0x01 ) == 0x01 )

```

Which is unambiguous in any language

---

<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 October 2022 11:12 UTC](https://discourse.nodered.org/t/bitwise-syntax-error-error-in-javascript-code/68466/13 "2022-10-02T11:12:09Z")

</div>

Or, if you want 1/0 rather than true/false use  
`msg.zone = (msg.payload[1 + 1] >> 2) & 0x01`

---

<div class="post-metadata">

**Author:** ![M-a-r-t-i-n](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/m-a-r-t-i-n/32/52391_2.png) [@M-a-r-t-i-n](https://discourse.nodered.org/u/M-a-r-t-i-n)\
**Post date:** [2 October 2022 11:12 UTC](https://discourse.nodered.org/t/bitwise-syntax-error-error-in-javascript-code/68466/14 "2022-10-02T11:12:41Z")

</div>

Well, yes, had it been my code, that’s what I would have done.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [2 October 2022 13:31 UTC](https://discourse.nodered.org/t/bitwise-syntax-error-error-in-javascript-code/68466/15 "2022-10-02T13:31:28Z")

</div>

Or even

```auto
msg.zone = (msg.payload[1 + 1] >> 2) & 1

```

🙂

---

<div class="post-metadata">

**Author:** ![walberjunior](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/walberjunior/32/68528_2.png) [@walberjunior](https://discourse.nodered.org/u/walberjunior)\
**Post date:** [2 October 2022 21:17 UTC](https://discourse.nodered.org/t/bitwise-syntax-error-error-in-javascript-code/68466/16 "2022-10-02T21:17:25Z")

</div>

> [@walberjunior](#):
>
> I have the code below in a function, but even though it works as I expect, the function gets "error in JavaScript code".

I think you are missing the point, where the code works. The problem is just the error message present in Node Red.

> [@TotallyInformation](#):
>
> `msg.zone = (msg.payload[1 + 1] >> 2) & 1`

It works too, but the error message is still present

> [@Colin](#):
>
> `msg.zone = (msg.payload[1 + 1] >> 2) & 0x01`

It works too, but the error message is still present

> [@Colin](#):
>
> I don't understand what you mean. The code `(0x01 == 0x01)` will always return the value `true`, since obviously 0x01 is always equal to 0x01.

Like I said, I'm not good with programming. I don't know why, but it returns 1 or 0 with the code below.  
Each byte represents a group of 8 zones from my alarm, each bit represents 1 zone.  
If zone 19 is open (buffer[4] bit 2)  
`msg.zone = ((msg.payload[3 + 1] >> 2) & (0x01 == 0x01))` this returns 1  
If zone 19 is closed (buffer[4] bit 2)  
`msg.zone = ((msg.payload[3 + 1] >> 2) & (0x01 == 0x01))` this returns 0

 ![node6](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/0/002514162f652039e7328e23f3edad042bd69842.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:** [3 October 2022 07:39 UTC](https://discourse.nodered.org/t/bitwise-syntax-error-error-in-javascript-code/68466/17 "2022-10-03T07:39:35Z")

</div>

> [@walberjunior](#):
>
> The problem is just the error message present in Node Red.

Please select that function node and Export it and paste it here.

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote `````)

````auto
``` 
   code goes here 
```

````

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<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:** [3 October 2022 07:50 UTC](https://discourse.nodered.org/t/bitwise-syntax-error-error-in-javascript-code/68466/18 "2022-10-03T07:50:03Z")

</div>

If you are simply wanting to extract data from a buffer, then the buffer-parser node is designed to do this the "no-code" way...

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

```auto
[{"id":"9b8474f8aa18762a","type":"buffer-parser","z":"4c5ad8c7caa80822","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"bool","name":"zone19open","offset":4,"length":1,"offsetbit":2,"scale":"1","mask":""},{"type":"bool","name":"zone20open","offset":4,"length":1,"offsetbit":3,"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":2150,"y":860,"wires":[["f39a39559f771680"]]},{"id":"e403c7115f8776c7","type":"inject","z":"4c5ad8c7caa80822","name":"simulating your data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[\"0x37\",\"0x39\",\"0x0\",\"0x0\",\"0x4\"]","payloadType":"bin","x":1950,"y":860,"wires":[["9b8474f8aa18762a"]]},{"id":"f39a39559f771680","type":"debug","z":"4c5ad8c7caa80822","name":"debug 94","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2310,"y":860,"wires":[]},{"id":"83d494286ca2baa4","type":"inject","z":"4c5ad8c7caa80822","name":"simulating your data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[\"0x37\",\"0x39\",\"0x0\",\"0x0\",\"0x8\"]","payloadType":"bin","x":1950,"y":920,"wires":[["9b8474f8aa18762a"]]}]

```

---

<div class="post-metadata">

**Author:** ![Frida](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/frida/32/68250_2.png) [@Frida](https://discourse.nodered.org/u/Frida)\
**Post date:** [3 October 2022 14:01 UTC](https://discourse.nodered.org/t/bitwise-syntax-error-error-in-javascript-code/68466/19 "2022-10-03T14:01:31Z")

</div>

When I insert into a function block I also get the error.  
But if I put an extra & in, see the next function, there  
are no errors.

 ![pic01](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/c/fc345c4f7d547501be92ecb6536f757d3c15f321.png)

```auto
[{"id":"87969c3818de0019","type":"function","z":"f4b8c2e3d4ea3ab1","name":"function 25","func":"msg.zone = ((msg.payload[1 + 1] >> 2) & (0x01 == 0x01));\n","outputs":1,"noerr":1,"initialize":"","finalize":"","libs":[],"x":290,"y":1300,"wires":[[]]},{"id":"531d304dcec94662","type":"function","z":"f4b8c2e3d4ea3ab1","name":"function 26","func":"msg.zone = ((msg.payload[1 + 1] >> 2) && (0x01 == 0x01));\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":290,"y":1360,"wires":[[]]},{"id":"935753bc85eadea1","type":"comment","z":"f4b8c2e3d4ea3ab1","name":"msg.zone = ((msg.payload[1 + 1] >> 2) & (0x01 == 0x01));","info":"","x":650,"y":1300,"wires":[]},{"id":"c6638f8f65e78844","type":"comment","z":"f4b8c2e3d4ea3ab1","name":"msg.zone = ((msg.payload[1 + 1] >> 2) && (0x01 == 0x01));","info":"","x":650,"y":1360,"wires":[]}]

```

---

<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:** [3 October 2022 14:09 UTC](https://discourse.nodered.org/t/bitwise-syntax-error-error-in-javascript-code/68466/20 "2022-10-03T14:09:42Z")

</div>

> [@Frida](#):
>
> `((msg.payload[1 + 1] >> 2) & (0x01 == 0x01))`

The right hand part `(0x01 == 0x01)` evaluates to a Boolean & cannot be used with the bitwise `&` operator.

If you hover on the red squiggle in the code editor, what does it say?

[Next page](https://discourse.nodered.org/t/bitwise-syntax-error-error-in-javascript-code/68466.md?page=2)
