Help with bitwise

Hi All,

I need some help converting a long 2711790500 (0xA1A2A3A4) to 2762187425 (0xA4A3A3A1)

Here is the code I am trying...

void setup() 
{
  Serial.begin(9600);
  unsigned long n = 2711790500;
  byte buf[4]; 
  buf[0] = byte(n);
  buf[1] = byte(n >> 8);
  buf[2] = byte(n >> 16);
  buf[3] = byte(n  >> 24);
  unsigned long value = (unsigned long) (buf[0] << 24) | (unsigned long) (buf[1] << 16) | (unsigned long) (buf[2] << 8) | (unsigned long) buf[3];
  Serial.println(value, HEX);

}

void loop() {
  // put your main code here, to run repeatedly:

}

This seems to give me an output of 0xFFFFA2A1.

I'm struggling to see what I'm doing wrong can anybody help?

Thanks

Try ((unsigned long)buf[0] << 24) etc.
Did you consider to use the union?

Why make it so complicated?

uint32_t x = 2711790500UL;

x = 2762187425UL;

Regards,
Ray L.

Ah yes "UL", sry. :-[

Look at this or even this.

Hi All,

Thanks for all of your help.

In the end I went with this...

uint32_t byteReverse(uint32_t value)
{
    uint32_t result = 0;
    result |= (value & 0x000000FF) << 24;
    result |= (value & 0x0000FF00) << 8;
    result |= (value & 0x00FF0000) >> 8;
    result |= (value & 0xFF000000) >> 24;
    return result;
}

Thanks again
Richard