# Decrypting messages for dummies

**URL:** <https://www.thethingsnetwork.org/forum/t/decrypting-messages-for-dummies/4894>\
**Category:** Uncategorized\
**Created:** [January 17, 2017, 8:47pm UTC](https://www.thethingsnetwork.org/forum/t/decrypting-messages-for-dummies/4894 "2017-01-17T20:47:54Z")\
**Posts on this page:** 1\
**Showing post:** 36

<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:** [July 24, 2020, 8:28am UTC](https://www.thethingsnetwork.org/forum/t/decrypting-messages-for-dummies/4894/36 "2020-07-24T08:28:10Z")

</div>

By the way:

> [@HTLorawan](#):
>
> When I use this (from your earlier post upstream):
> 
> > decoded.tPitch = ( (bytes[13] & 0x80 ? 0xFFFF\<\<24 : 0) | bytes[13]\<\<16 | bytes[14]\<\<8 | bytes[15]) / 10e8 \* 1000.0;
> 
> it obtains the correct result: “tPitch”: -0.612914

While it will indeed return the correct value, using `0xFFFF<<24` is confusing, I feel. I wrote:

> [@arjanvanb](#):
>
> ```javascript
> // Sign-extend 16 bits to 32 bits
> // [...]
> var celciusInt = (bytes[0] & 0x80 ? 0xFFFF<<16 : 0) | bytes[0]<<8 | bytes[1];
> 
> ```

This is the same as:

```javascript
var celciusInt = (bytes[0] & 0x80 ? 0xFFFF0000 : 0) | bytes[0]<<8 | bytes[1];

```

Above, handling 2 bytes from the payload, one needs to determine the value for the leftmost 2 bytes to get 32 bits. But in your case, when handling 3 bytes from the payload, you only need 1 extra byte to get 32 bits. Now, in JavaScript’s 32 bits processing for bitwise operators, your `0xFFFF<<24` indeed yields the 32 bits `FF000000`, not the 40 bits `FFFF000000`. But you can achieve the same with `0xFF<<24`.

To be sure you understand bit shifting, see many more examples in [Laird RS1xx - probably a question on bit shifting - #7 by arjanvanb](https://www.thethingsnetwork.org/forum/t/laird-rs1xx-probably-a-question-on-bit-shifting/37808/7) and [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).

(Aside: 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) about formatting code blocks, to avoid surprises with unwanted formatting, like happened in your first post before I edited that.)

---

_[View the full topic](https://www.thethingsnetwork.org/forum/t/decrypting-messages-for-dummies/4894)._
