0

I'm having the following problem:

I have an uint8_t h_MSB and uint16_t h_LSB and iwant to combine them into a uint32_t

So here is my code:

void  parseHeader(MyPackage Header,uint32_t* timeStamp ){
(*timeStamp) = (Header->h_MSB <<16)| Header->h_LSB;
} 

But it does not seem to work;

I tried it with h_MSB = 10 and h_LSB= 10

I get 10 for the timestamp.

The problem seems to be that if I shift beyon 7 bit all information from h_MSB ist lost, but how can it be since timestamp is a uint32_t ?

AMIC MING
  • 6,306
  • 6
  • 46
  • 62
Anh Tuan Nguyen
  • 971
  • 3
  • 13
  • 28

1 Answers1

0

The problem is that h_MSB is uint8_t and the shift operation is performed within the uint8_t type (or possibly within uint16_t, but it doesn't matter), so you get 0. Cast it to uint32_t before shifting:

(*timeStamp) = (((uint32_t)Header->h_MSB) << 16) | Header->h_LSB;
maral
  • 1,431
  • 1
  • 10
  • 15