# How to set bits in a variable

**URL:** <https://discourse.nodered.org/t/how-to-set-bits-in-a-variable/38156>\
**Category:** General\
**Created:** [28 December 2020 20:30 UTC](https://discourse.nodered.org/t/how-to-set-bits-in-a-variable/38156 "2020-12-28T20:30:20Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [28 December 2020 20:30 UTC](https://discourse.nodered.org/t/how-to-set-bits-in-a-variable/38156/1 "2020-12-28T20:30:21Z")

</div>

I want to know how to un/set bits in a message/variable.

What I find is not clear for me to understand.

---

<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:** [28 December 2020 20:56 UTC](https://discourse.nodered.org/t/how-to-set-bits-in-a-variable/38156/2 "2020-12-28T20:56:11Z")

</div>

> [@Node-red-contrib-remote-io, digital output for joining bool to word](https://discourse.nodered.org/t/node-red-contrib-remote-io-digital-output-for-joining-bool-to-word/11798/2):
>
> If you mean combining bits to make a WORD value - its just basic math functionality of the programming language. e.g.... //Get bit function getBit(number, bitPosition) { return (number & (1 \<\< bitPosition)) === 0 ? 0 : 1; } //Set Bit function setBit(number, bitPosition) { return number | (1 \<\< bitPosition); } //Clear Bit function clearBit(number, bitPosition) { const mask = ~(1 \<\< bitPosition); return number & mask; } //Update Bit function updateBit(number, bitPosition, bitValue) { …

Any good?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [28 December 2020 21:13 UTC](https://discourse.nodered.org/t/how-to-set-bits-in-a-variable/38156/3 "2020-12-28T21:13:57Z")

</div>

Thanks, but _Mr Stupid_ is not really getting it.

Sorry, but could you give a better example of how to set a bit?

For the sake of it: bit 2.

Appreciated.

---

<div class="post-metadata">

**Author:** ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)\
**Post date:** [28 December 2020 21:20 UTC](https://discourse.nodered.org/t/how-to-set-bits-in-a-variable/38156/4 "2020-12-28T21:20:29Z")

</div>

There is a node contrib-bitunloader that does a lot of what you are asking. Even if you don't use the node itself you could crack it open and look at the code. NOTE: it also loads a second node called bit unloader that may provide useful code

---

<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:** [28 December 2020 21:23 UTC](https://discourse.nodered.org/t/how-to-set-bits-in-a-variable/38156/5 "2020-12-28T21:23:18Z")

</div>

> [@Trying\_to\_learn](#):
>
> For the sake of it: bit 2.

```javascript
function setBit(number, bitPosition) {
  return number | (1 << bitPosition);
}

var currentVal = 0;
var newVal = setBit(currentVal, 2)

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [28 December 2020 21:24 UTC](https://discourse.nodered.org/t/how-to-set-bits-in-a-variable/38156/6 "2020-12-28T21:24:06Z")

</div>

So I need to put the `function` above the code, not below?

(Thanks. Shall try now)

---

<div class="post-metadata">

**Author:** ![molesworth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/molesworth/32/11748_2.png) [@molesworth](https://discourse.nodered.org/u/molesworth)\
**Post date:** [28 December 2020 21:24 UTC](https://discourse.nodered.org/t/how-to-set-bits-in-a-variable/38156/7 "2020-12-28T21:24:26Z")

</div>

As a professional bit-twiddler (writing device drivers and the like) I can say it's a skill worth learning for anything to do with low-level coding.

I think @Steve-Mcl has provided the basic functions above, but additionally it's worth learning how the bit values go, and ways to represent them.

Decimal probably isn't the easiest way to understand them. Although it starts easily with 1, 2, 4, 8 etc., by the time you get up to 16384, 32768 and so on, it's hard to keep track.

Hexadecimal ("hex") is a lot easier, since it goes in groups of 4 bits 0x01, 0x02, 0x04, 0x08, then shift to the next group - 0x10, 0x20, 0x40, 0x80, and so on.

Something else I also use regularly is constants for specific bit positions or bit-fields, e.g.

`const WRITE_ENABLE = 0x0200;`

then use the name rather than the value (which you'll forget or get wrong occasionally anyway).

Once you get your head round the basics it'll all fall into place, but just ask if you get stuck.

---

<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:** [28 December 2020 21:24 UTC](https://discourse.nodered.org/t/how-to-set-bits-in-a-variable/38156/8 "2020-12-28T21:24:34Z")

</div>

> [@Trying\_to\_learn](#):
>
> So I need to put the `function` above the code, not below?

Doesn't matter

[Understanding Hoisting in JavaScript | DigitalOcean](https://www.digitalocean.com/community/tutorials/understanding-hoisting-in-javascript#:~:text=Hoisting%20is%20a%20JavaScript%20mechanism,scope%20is%20global%20or%20local).

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [28 December 2020 21:32 UTC](https://discourse.nodered.org/t/how-to-set-bits-in-a-variable/38156/9 "2020-12-28T21:32:55Z")

</div>

@molesworth  
Thanks.

Yes, I do get what you are saying. But I am facing a problem (see other thread if you want) and it is becoming painfully clear I need to set bits to indicate status.

So I am going to use bits to reflect the status of the devices.

Yes, I need to learn about it more but for me at this stage, I am not wanting to bite off more than I can chew - though I guess I kind of have if I am asking the question.

@Steve-Mcl  
Thanks again.

I got tripped with how to call the function but with that latest post, I have it working in a way I like.

I'll put some effort now into implementing what I just learnt into application.

---

<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:** [11 January 2021 21:33 UTC](https://discourse.nodered.org/t/how-to-set-bits-in-a-variable/38156/10 "2021-01-11T21:33:09Z")

</div>

Closed on 11 January 2021 21:33 UTC

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