Bitwise syntax error ("error in JavaScript code")

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.

1 Like

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.

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.

I suspect it is doing bitwise & followed by equality == then shift >>
See here: Operator precedence - JavaScript | MDN

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

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));

In that case, perhaps your code should read:

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

Which is unambiguous in any language

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

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

Or even

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

:slight_smile:

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

It works too, but the error message is still present

It works too, but the error message is still present

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

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

``` 
   code goes here 
```

See this post for more details - How to share code or flow json

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...

[{"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"]]}]
1 Like

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.

[{"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":[]}]

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?

As we have said multiple times, that line should be
msg.zone = ((msg.payload[1 + 1] >> 2) & 1) == 1
if you want true/false, or
msg.zone = (msg.payload[1 + 1] >> 2) & 1
if you want 1/0

Urm, actually it can according to MDN which states that the bitwise operators convert their operands to 32bit integers. As I've previously stated several times now, that makes (0x01 == 0x01) === true === 1.

So that means that Monaco is being somewhat boorish here by showing an error when it should be showing a warning.

Non-the-less, again, as stated many times now, the original equation does not make sense and doesn't do what was intended.

Yeah that's what I meant Julian. I should probably have been more explicit.

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