HEX Multiplication

Hi

I want to know why when I Serial print below code I am not getting the whole result:

EX: 0x9e3779b9*32 = 0x13C6EF3720 but I am only getting 0xC6EF3720 which is missing the 9bit

uint32_t sum = 0x9e3779b9*32;
Serial.println(sum ,HEX);

Thank you

The multiplication results exceeds the the highest number that can be represented by a 32-bit integer.

Also,

uint32_t sum = 0x9e3779b9*32;

The multiplication is only being done with 32-bit arithmetic.

void setup() 
{
  Serial.begin(9600);
  union
  {
    uint64_t x;
    uint16_t myData[4];
  } data;

  data.x = (uint64_t) 0x9e3779b9 * 32; //0x13C6EF3720
  Serial.print(data.myData[2], HEX); 
  Serial.print(data.myData[1], HEX); 
  Serial.println(data.myData[0], HEX);
}

void loop() 
{
  
}

This:

 Serial.print(data.myData[2], HEX);
  Serial.print(data.myData[1], HEX);
  Serial.println(data.myData[0], HEX);

won't print leading 0s for data.myData[1] and data.myData[0] so it probably won't look right.

The Serial Monitor shows:
smar.png

smar.png

Try different numbers that have leading zeros in data.myData[1] and data.myData[0] meaning the 16 bit value is less than 0x1000. It's not a very good solution if it only works for some numbers.

How about this one?
smat.png

void setup() 
{
  Serial.begin(9600);
  union
  {
    uint64_t x;
    uint8_t myData[8];
  } data;

  data.x = (uint64_t) 0x9e3779b9 * 32; //0x13C6EF3720
  for(int i=7; i>=0; i--)
  {
    byte x = data.myData[i];
    if(x <0x10)
    {
      Serial.print('0');
    }
    Serial.print(x, HEX);
  }
  Serial.println();
}

void loop() 
{
  
}

smat.png

Did you test it with numbers as specified below? Did it work? If so, then OK.

gfvalvo:
Try different numbers that have leading zeros in data.myData[1] and data.myData[0] meaning the 16 bit value is less than 0x1000. It's not a very good solution if it only works for some numbers.

The sketch of Post#6 is now checking leading 0 of every byte of the result. Therefore, the print() method would show the leading 0s.

Tested for:
0x0102*0x0102 = 0x10404
smny.png

How to discard all those meaningless and odd looking 0s that are appearing at the very left of the result?

smny.png

Don't print anything until you find the first non-zero byte. Then print all bytes (with added leading zeros as necessary).

Thanks for all the replay, The reason I was asking this was that I am using below code to encrypt message using xtea(below code)

but I do not think the encryption is correct because the multiplication result was wrong.

void encipher(unsigned int num_rounds, uint32_t v[2], uint32_t const key[4]) {
  unsigned int i;
  uint32_t v0 = v[0], v1 = v[1], sum = 0, delta = 0x9E3779B9;
  for (i = 0; i < num_rounds; i++) {
    v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]);
         
              sum += delta;

    v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum >> 11) & 3]);
   
  }

  v[0] = v0; v[1] = v1;

}

as you can see sum is equal numround which 32 times by delta

This might be a case of misdiagnosing a problem (assuming there is a problem in the first place). It's pretty common for bit-twiddling algorithms to discard the upper bits of arithmetic operations, since they're not needed.

What leads you to believe that that encryption function is not working correctly?

There’s another active thread by the OP on this subject.

Can you provide a link for that thread

I know is wrong since when I encrypt it different language like php, Java it different.

Thank you