Rotate function not working for uint3_t?

When I wrtie a functionn to rotate uint16_t it works as expected

#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

#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?

Repeated rotation just sift the contents out to the left

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.

what microcontroller are you using?

Have edited OP accoringly

Nano

INT_BITS_32 is not the same as IN_BITS_32

can you post the real code ?

1 Like

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

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

ATMega328P

can you post your full code printing the details

Worth a try:

#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?

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

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

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

try

#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

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

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

Yes I get the exact same output

so all works :slight_smile:

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

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.