# Rotate function not working for uint3\_t?

**URL:** <https://forum.arduino.cc/t/rotate-function-not-working-for-uint3-t/1406256>\
**Category:** Programming\
**Created:** [September 14, 2025, 4:04pm UTC](https://forum.arduino.cc/t/rotate-function-not-working-for-uint3-t/1406256 "2025-09-14T16:04:52Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![DrBwts](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drbwts/32/1098690_2.png) [@DrBwts](https://forum.arduino.cc/u/DrBwts)\
**Post date:** [September 14, 2025, 4:04pm UTC](https://forum.arduino.cc/t/rotate-function-not-working-for-uint3-t/1406256/1 "2025-09-14T16:04:52Z")

</div>

When I wrtie a functionn to rotate uint16\_t it works as expected

```arduino
#define INT_BITS 16
/* rotate n by d bits*/
int leftRotate(uint16_t n, uint16_t d)
{
    return (n << d) | (n >> (INT_BITS - d));
}

```

But when I do the same for uint32\_t

```arduino
#define INT_BITS_32 32

uint32_t leftRotate_uint32(uint32_t n, uint32_t d)
{
    return (n << d) | (n >> (INT_BITS_32 - d));
}

```

This just results in a left shift?

Any clues anyone?

```arduino
Repeated rotation just sift the contents out to the left

```

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/7/b/a/7ba8beb8240311ce94b68901180c9c39aa68ea63.png)

---

<div class="post-metadata">

**Author:** ![van\_der\_decken](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/van_der_decken/32/1006976_2.png) [@van\_der\_decken](https://forum.arduino.cc/u/van_der_decken)\
**Post date:** [September 14, 2025, 4:06pm UTC](https://forum.arduino.cc/t/rotate-function-not-working-for-uint3-t/1406256/2 "2025-09-14T16:06:54Z")

</div>

Perhaps begin by printing out the values of the partial expressions to see what's happening.

Edit: and by the way, where is `n` defined in `leftRotate_uint32`? You use it but I don't see a definition for it.

---

<div class="post-metadata">

**Author:** ![horace](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/horace/32/1014299_2.png) [@horace](https://forum.arduino.cc/u/horace)\
**Post date:** [September 14, 2025, 4:08pm UTC](https://forum.arduino.cc/t/rotate-function-not-working-for-uint3-t/1406256/3 "2025-09-14T16:08:34Z")

</div>

what microcontroller are you using?

---

<div class="post-metadata">

**Author:** ![DrBwts](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drbwts/32/1098690_2.png) [@DrBwts](https://forum.arduino.cc/u/DrBwts)\
**Post date:** [September 14, 2025, 4:11pm UTC](https://forum.arduino.cc/t/rotate-function-not-working-for-uint3-t/1406256/4 "2025-09-14T16:11:11Z")

</div>

Have edited OP accoringly

---

<div class="post-metadata">

**Author:** ![DrBwts](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drbwts/32/1098690_2.png) [@DrBwts](https://forum.arduino.cc/u/DrBwts)\
**Post date:** [September 14, 2025, 4:11pm UTC](https://forum.arduino.cc/t/rotate-function-not-working-for-uint3-t/1406256/5 "2025-09-14T16:11:20Z")

</div>

Nano

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [September 14, 2025, 4:14pm UTC](https://forum.arduino.cc/t/rotate-function-not-working-for-uint3-t/1406256/6 "2025-09-14T16:14:04Z")

</div>

> [@DrBwts](#):
>
> ```arduino
> #define IN_BITS_32 32
> 
> ```

> [@DrBwts](#):
>
> ` return (n << d) | (n >> (INT_BITS_32 - d));`

INT\_BITS\_32 is not the same as IN\_BITS\_32

can you post the real code ?

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [September 14, 2025, 4:14pm UTC](https://forum.arduino.cc/t/rotate-function-not-working-for-uint3-t/1406256/7 "2025-09-14T16:14:54Z")

</div>

Nano V3 (ATMEGA328)  
Nano Every  
Nano 33 IoT  
Nano 33 BLE  
Nano 33 BLE Sense  
Nano RP2040 Connect  
Nano 33 BLE Sense Rev 2  
Nano ESP32  
Nano R4

It makes a difference

---

<div class="post-metadata">

**Author:** ![DrBwts](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drbwts/32/1098690_2.png) [@DrBwts](https://forum.arduino.cc/u/DrBwts)\
**Post date:** [September 14, 2025, 4:15pm UTC](https://forum.arduino.cc/t/rotate-function-not-working-for-uint3-t/1406256/8 "2025-09-14T16:15:05Z")

</div>

Typo in the OP that isnt in the actual code will fix now.

---

<div class="post-metadata">

**Author:** ![DrBwts](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drbwts/32/1098690_2.png) [@DrBwts](https://forum.arduino.cc/u/DrBwts)\
**Post date:** [September 14, 2025, 4:16pm UTC](https://forum.arduino.cc/t/rotate-function-not-working-for-uint3-t/1406256/9 "2025-09-14T16:16:40Z")

</div>

ATMega328P

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [September 14, 2025, 4:17pm UTC](https://forum.arduino.cc/t/rotate-function-not-working-for-uint3-t/1406256/10 "2025-09-14T16:17:32Z")

</div>

can you post your full code printing the details

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [September 14, 2025, 4:19pm UTC](https://forum.arduino.cc/t/rotate-function-not-working-for-uint3-t/1406256/11 "2025-09-14T16:19:00Z")

</div>

Worth a try:

```arduino
#define INT_BITS_32 32UL

uint32_t leftRotate_uint32(uint32_t n, uint32_t d)
{
    return (n << d) | (n >> (INT_BITS_32 - d));
}

```

Do you have any other Arduino, with 32-bit chip? What happens with your code on that board?

---

<div class="post-metadata">

**Author:** ![DrBwts](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drbwts/32/1098690_2.png) [@DrBwts](https://forum.arduino.cc/u/DrBwts)\
**Post date:** [September 14, 2025, 4:20pm UTC](https://forum.arduino.cc/t/rotate-function-not-working-for-uint3-t/1406256/12 "2025-09-14T16:20:56Z")

</div>

My compiler threw a fit when I added that, nope just a couple of nanos

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [September 14, 2025, 4:23pm UTC](https://forum.arduino.cc/t/rotate-function-not-working-for-uint3-t/1406256/13 "2025-09-14T16:23:42Z")

</div>

> [@DrBwts](#):
>
> threw a fit

Nope. No emotional responses. Maybe error or warning messages. You could post them (use code tags!).

---

<div class="post-metadata">

**Author:** ![DrBwts](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drbwts/32/1098690_2.png) [@DrBwts](https://forum.arduino.cc/u/DrBwts)\
**Post date:** [September 14, 2025, 4:25pm UTC](https://forum.arduino.cc/t/rotate-function-not-working-for-uint3-t/1406256/14 "2025-09-14T16:25:01Z")

</div>

_D:\My Pics\Pacman\pac\_code\pac\_code.ino: In function 'uint32\_t leftRotate\_uint32(uint32\_t, uint32\_t)':  
D:\My Pics\Pacman\pac\_code\pac\_code.ino:7:26: error: expected ')' before numeric constant  
 #define INT\_BITS\_32 32 32UL  
^  
D:\My Pics\Pacman\pac\_code\pac\_code.ino:265:30: note: in expansion of macro 'INT\_BITS\_32'  
return (n \<\< d) | (n \>\> (INT\_BITS\_32 - d));  
^ ~~~~~~~~~~  
D:\My Pics\Pacman\pac\_code\pac\_code.ino:265:47: error: expected ')' before ';' token  
return (n \<\< d) | (n \>\> (INT\_BITS\_32 - d));  
^  
D:\My Pics\Pacman\pac\_code\pac\_code.ino: In function 'uint32\_t rightRotate\_int32(uint32\_t, uint32\_t)':  
D:\My Pics\Pacman\pac\_code\pac\_code.ino:7:26: error: expected ')' before numeric constant  
 #define INT\_BITS\_32 32 32UL  
^  
D:\My Pics\Pacman\pac\_code\pac\_code.ino:272:30: note: in expansion of macro 'INT\_BITS\_32'  
return (n \>\> d) | (n \<\< (INT\_BITS\_32 - d));  
^ ~~~~~~~~~~  
D:\My Pics\Pacman\pac\_code\pac\_code.ino:272:47: error: expected ')' before ';' token  
return (n \>\> d) | (n \<\< (INT\_BITS\_32 - d));  
^_

_exit status 1_

_Compilation error: expected ')' before numeric constant_

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [September 14, 2025, 4:25pm UTC](https://forum.arduino.cc/t/rotate-function-not-working-for-uint3-t/1406256/15 "2025-09-14T16:25:57Z")

</div>

try

```arduino
#define INT_BITS_32 32
uint32_t leftRotate_uint32(uint32_t n, uint32_t d) {
  return (n << d) | (n >> (INT_BITS_32 - d));
}

void printBinary32(uint32_t value) {
  for (int8_t i = 31; i >= 0; i--) Serial.print(bitRead(value, i));
  Serial.println();
}

void setup() {
  Serial.begin(115200);
  uint32_t value = 0b1111111111111111;

  for (uint32_t i = 0; i < INT_BITS_32; i++) {
    uint32_t rotated = leftRotate_uint32(value, i);
    Serial.print("Rotate by ");
    if (i < 10) Serial.write(' ');
    Serial.print(i);
    Serial.print(": ");
    printBinary32(rotated);
  }
}

void loop() {}

```

you should see

```plaintext
Rotate by 0: 00000000000000001111111111111111
Rotate by 1: 00000000000000011111111111111110
Rotate by 2: 00000000000000111111111111111100
Rotate by 3: 00000000000001111111111111111000
Rotate by 4: 00000000000011111111111111110000
Rotate by 5: 00000000000111111111111111100000
Rotate by 6: 00000000001111111111111111000000
Rotate by 7: 00000000011111111111111110000000
Rotate by 8: 00000000111111111111111100000000
Rotate by 9: 00000001111111111111111000000000
Rotate by 10: 00000011111111111111110000000000
Rotate by 11: 00000111111111111111100000000000
Rotate by 12: 00001111111111111111000000000000
Rotate by 13: 00011111111111111110000000000000
Rotate by 14: 00111111111111111100000000000000
Rotate by 15: 01111111111111111000000000000000
Rotate by 16: 11111111111111110000000000000000
Rotate by 17: 11111111111111100000000000000001
Rotate by 18: 11111111111111000000000000000011
Rotate by 19: 11111111111110000000000000000111
Rotate by 20: 11111111111100000000000000001111
Rotate by 21: 11111111111000000000000000011111
Rotate by 22: 11111111110000000000000000111111
Rotate by 23: 11111111100000000000000001111111
Rotate by 24: 11111111000000000000000011111111
Rotate by 25: 11111110000000000000000111111111
Rotate by 26: 11111100000000000000001111111111
Rotate by 27: 11111000000000000000011111111111
Rotate by 28: 11110000000000000000111111111111
Rotate by 29: 11100000000000000001111111111111
Rotate by 30: 11000000000000000011111111111111
Rotate by 31: 10000000000000000111111111111111

```

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [September 14, 2025, 4:28pm UTC](https://forum.arduino.cc/t/rotate-function-not-working-for-uint3-t/1406256/16 "2025-09-14T16:28:42Z")

</div>

> [@DrBwts](#):
>
> ```arduino
> Repeated rotation just sift the contents out to the left
> 
> ```
> 
> ![image](https://europe1.discourse-cdn.com/arduino/original/4X/7/b/a/7ba8beb8240311ce94b68901180c9c39aa68ea63.png)

Looks like your code is using 16-bit integer for printing, are you actually sending a 32-bit value to the function?

---

<div class="post-metadata">

**Author:** ![DrBwts](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drbwts/32/1098690_2.png) [@DrBwts](https://forum.arduino.cc/u/DrBwts)\
**Post date:** [September 14, 2025, 4:30pm UTC](https://forum.arduino.cc/t/rotate-function-not-working-for-uint3-t/1406256/17 "2025-09-14T16:30:35Z")

</div>

Yes I get the exact same output

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [September 14, 2025, 4:31pm UTC](https://forum.arduino.cc/t/rotate-function-not-working-for-uint3-t/1406256/18 "2025-09-14T16:31:31Z")

</div>

so all works 🙂

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [September 14, 2025, 4:35pm UTC](https://forum.arduino.cc/t/rotate-function-not-working-for-uint3-t/1406256/19 "2025-09-14T16:35:31Z")

</div>

Ok, be like that, no more help from me.

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [September 14, 2025, 4:38pm UTC](https://forum.arduino.cc/t/rotate-function-not-working-for-uint3-t/1406256/20 "2025-09-14T16:38:00Z")

</div>

> [@DrBwts](#):
>
> #define INT\_BITS\_32 32 32UL

Of course that causes warning, that replaces "INT\_BITS\_32" with "32 32UL", the compiler does not like the resulting two integers where it expects only one.

[Next page](https://forum.arduino.cc/t/rotate-function-not-working-for-uint3-t/1406256.md?page=2)
