Convert a number to uint64_t doesn't work

Dear support

I need to pass 128 bits service uuid to discovery process as variable, I have an array of 16 bytes of uuid number, it is divided into five parts as show here

BT_UUID_128_ENCODE(w32, w1, w2, w3, w48)
where w48 is 48 bits number to pass to, so I have to define a uint16_t variable(w48) to hold it,  and convert from my byte array (value[ ]) as show below:
uint64_t w48;
w48 = value[10]<<40 + value[11]<<32;   //this line ends up 0
w48 += value[12]<<24+value[13]<<16;
w48 +=value[14]<<8 + value[15];
But the first line doesn't work, it ends up 0, while the other 2 lines works individually. let me know what I can do please?
Thank you!
Ping
Parents
  • Hi,

     

    The compiler (gcc in my test) should warn you when an overflow occurs, like in this case:

    warning: left shift count >= width of type [-Wshift-count-overflow]
       15 |  w48 = (value[10]<<40) + (value[11]<<32);   //this line ends up 0
          |                  ^~
    warning: left shift count >= width of type [-Wshift-count-overflow]
       15 |  w48 = (value[10]<<40) + (value[11]<<32);   //this line ends up 0

    Can you try casting value[10] and [11] to uint64_t to see if this fixes the issue?

     

    Kind regards,

    Håkon

Reply
  • Hi,

     

    The compiler (gcc in my test) should warn you when an overflow occurs, like in this case:

    warning: left shift count >= width of type [-Wshift-count-overflow]
       15 |  w48 = (value[10]<<40) + (value[11]<<32);   //this line ends up 0
          |                  ^~
    warning: left shift count >= width of type [-Wshift-count-overflow]
       15 |  w48 = (value[10]<<40) + (value[11]<<32);   //this line ends up 0

    Can you try casting value[10] and [11] to uint64_t to see if this fixes the issue?

     

    Kind regards,

    Håkon

Children
Related