# Why to use & 0xff when bitshifting.?

**URL:** <https://forum.arduino.cc/t/why-to-use-0xff-when-bitshifting/587761>\
**Category:** Programming\
**Created:** [April 24, 2019, 1:12am UTC](https://forum.arduino.cc/t/why-to-use-0xff-when-bitshifting/587761 "2019-04-24T01:12:50Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![MorganS](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/morgans/32/218718_2.png) [@MorganS](https://forum.arduino.cc/u/MorganS)\
**Post date:** [April 24, 2019, 5:59pm UTC](https://forum.arduino.cc/t/why-to-use-0xff-when-bitshifting/587761/21 "2019-04-24T17:59:43Z")

</div>

And sometimes you need to promote to a "higher" type inside an expression even though the final result will fit into 16 bits.

---

<div class="post-metadata">

**Author:** ![Coding\_Badly](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@Coding\_Badly](https://forum.arduino.cc/u/Coding_Badly)\
**Post date:** [April 24, 2019, 6:02pm UTC](https://forum.arduino.cc/t/why-to-use-0xff-when-bitshifting/587761/22 "2019-04-24T18:02:30Z")

</div>

@Montmorency, we should use auto (post #10) and casting from the original post is unnecessary (argued repeatedly).

I wonder what happens when a newbie blindly accepts both of your absolutes?

> Montmorency:  
> The point is that you have to ... strive to use casts it only when they are necessary.

No, we don't.

If a typecast is unnecessary but we include it then no harm done (other than to offend **your** sensibilities).

If a typecast was necessary and we included it then good on us. Even if what we did was accidentally correct it is still correct.

---

<div class="post-metadata">

**Author:** ![Montmorency](https://avatars.discourse-cdn.com/v4/letter/m/76d3ee/32.png) [@Montmorency](https://forum.arduino.cc/u/Montmorency)\
**Post date:** [April 24, 2019, 6:05pm UTC](https://forum.arduino.cc/t/why-to-use-0xff-when-bitshifting/587761/23 "2019-04-24T18:05:06Z")

</div>

> MorganS:  
> And sometimes you need to promote to a "higher" type inside an expression even though the final result will fit into 16 bits.

Exactly. Also, that classic newbie question that has been asked on the Net a gazillion times already: "`int a = 1, b = 2; double c = a/b;` How come I don't get `0.5` in `c`?"

---

<div class="post-metadata">

**Author:** ![Montmorency](https://avatars.discourse-cdn.com/v4/letter/m/76d3ee/32.png) [@Montmorency](https://forum.arduino.cc/u/Montmorency)\
**Post date:** [April 24, 2019, 6:08pm UTC](https://forum.arduino.cc/t/why-to-use-0xff-when-bitshifting/587761/24 "2019-04-24T18:08:13Z")

</div>

> [@](#):
>
> If a typecast was necessary and we included it then good on us. Even if what we did was accidentally correct it is still correct.

As you mentioned yourself previously, it is not just a question of "correctness". It is a question of good programming practices, that cover not only correctness, bit also readability and maintainability of the code.

You can stuff a program choke full of "magic constants" and still make it correct. Nevertheless, "magic constants" is a bad programming practice. "Correctness" of the program does not redeem them in any way.

---

<div class="post-metadata">

**Author:** ![Coding\_Badly](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@Coding\_Badly](https://forum.arduino.cc/u/Coding_Badly)\
**Post date:** [April 24, 2019, 6:15pm UTC](https://forum.arduino.cc/t/why-to-use-0xff-when-bitshifting/587761/25 "2019-04-24T18:15:55Z")

</div>

> Montmorency:  
> You can stuff a program choke full of "magic constants" and still make it correct.

Wow. You really do struggle staying on topic. (I do wonder if that is purposeful.)

In any case, I won't be wasting anymore of my time.

If @jmusther wants more details they only have to ask.

---

<div class="post-metadata">

**Author:** ![Montmorency](https://avatars.discourse-cdn.com/v4/letter/m/76d3ee/32.png) [@Montmorency](https://forum.arduino.cc/u/Montmorency)\
**Post date:** [April 24, 2019, 6:20pm UTC](https://forum.arduino.cc/t/why-to-use-0xff-when-bitshifting/587761/26 "2019-04-24T18:20:53Z")

</div>

> [@](#):
>
> Wow. You really do struggle staying on topic. (I do wonder if that is purposeful.)

Wow. I never thought anybody would perceive a simple and appropriate _analogy_ as an attempt to deviate from the topic.

---

<div class="post-metadata">

**Author:** ![Coding\_Badly](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@Coding\_Badly](https://forum.arduino.cc/u/Coding_Badly)\
**Post date:** [April 24, 2019, 6:26pm UTC](https://forum.arduino.cc/t/why-to-use-0xff-when-bitshifting/587761/27 "2019-04-24T18:26:14Z")

</div>

The quote was meant as a single simple example to help you understand.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 11:47pm UTC](https://forum.arduino.cc/t/why-to-use-0xff-when-bitshifting/587761/28 "2021-05-05T23:47:10Z")

</div>



[Previous page](https://forum.arduino.cc/t/why-to-use-0xff-when-bitshifting/587761.md?page=1)
