# Laird RS1xx - probably a question on bit shifting

**URL:** <https://www.thethingsnetwork.org/forum/t/laird-rs1xx-probably-a-question-on-bit-shifting/37808>\
**Category:** End Devices (Nodes)\
**Created:** [June 15, 2020, 6:00pm UTC](https://www.thethingsnetwork.org/forum/t/laird-rs1xx-probably-a-question-on-bit-shifting/37808 "2020-06-15T18:00:04Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![arjanvanb](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/arjanvanb/32/46626_2.png) [@arjanvanb](https://www.thethingsnetwork.org/forum/u/arjanvanb)\
**Post date:** [June 16, 2020, 8:48am UTC](https://www.thethingsnetwork.org/forum/t/laird-rs1xx-probably-a-question-on-bit-shifting/37808/7 "2020-06-16T08:48:27Z")

</div>

Since you asked about bit shifting:

> [@JimK](#):
>
> ```javascript
> params.temperature = (bytes[5]<<24>>19);
> 
> ```

As a rule of thumb: when decoding numbers you’ll shift a full byte (8 bits), or sometimes a nibble (4 bits), or any multiples of that. So, the 19 bits above are really unexpected, and the result is basically the same as `bytes[5]<<5` along with [sign extension](https://en.wikipedia.org/wiki/Sign_extension). What were you trying to achieve there?

> [@JimK](#):
>
> ```javascript
> params.temp2 = (bytes[5]>>1 );
> 
> ```

This basically just divides the value by 2, discarding the rightmost bit. What were you expecting here?

If you were trying to decode _a single_ signed byte, then in JavaScript (which for bitwise operators [always assumes signed 32-bits integers](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Signed_32-bit_integers)) you could use:

```javascript
// Sign-extend a single byte to 32 bits to make JavaScript understand
// negative values, by shifting 24 bits to the left, followed by a
// sign-propagating right shift of the same number of bits.
params.temp_int = bytes[5]<<24>>24;

```

The above should correctly show you the integer part for both positive and negative temperatures. If you would be combining multiple bytes, the above translates to:

```javascript
// Signed integers, MSB
var int8 = bytes[0]<<24>>24;
var int16 = bytes[0]<<24>>16 | bytes[1];
var int24 = bytes[0]<<24>>8 | bytes[1]<<8 | bytes[2];
var int32 = bytes[0]<<24 | bytes[1]<<16 | bytes[2]<<8 | bytes[3];

```

I explained much more on shifting in [Payload Format / Decode for TM-901 / N1C2 sensor from KCS TraceME - #4 by arjanvanb](https://www.thethingsnetwork.org/forum/t/payload-format-decode-for-tm-901-n1c2-sensor-from-kcs-traceme/36313/4).

As for the ~~(unsigned)~~ decimal/fractional part, and even a full decoder for your device, search [is your friend](https://www.thethingsnetwork.org/forum/search?q=laird+rs1xx): see [Laird RS1xx payload format](https://www.thethingsnetwork.org/forum/t/laird-rs1xx-payload-format/19313). Laird is using a weird encoding for this one.

Asides: for the initialization, `var params = {};` really suffices, and please see [How do I format my forum post? [HowTo]](https://www.thethingsnetwork.org/forum/t/how-do-i-format-my-forum-post-howto/8735)

---

_[View the full topic](https://www.thethingsnetwork.org/forum/t/laird-rs1xx-probably-a-question-on-bit-shifting/37808)._
