Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

typecast and bit shift

Status
Not open for further replies.

TucsonDon

Member
C:
uint8_t data[4];
uint32_t time;

time = (uint32_t) ((data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]);

I want to shift the array into 32 byte variable, but all I get is data[0] at LSB. How do I typecast and shift?
 
You could put both types in a union, so the bytes in the int32 are individually accessible.

Or this may work (without verifying the syntax):
time = *(uint32 *) data;

But be wary of byte order..
 
You were left shifting a byte variable multiple bytes wider that
its type before you cast it into a 32 bit variable.

Cast the byte variable then do the shift and OR ?

Not a C expert here, still a novice.....:)


Regards, Dana.
 
rjenkinsgb time = *(uint32 *) data worked I just had to change the way that the data is loaded into the array (in reverse)
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top