Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining two uint8_t as uint16_t

Tags:

c

I have the following data

uint8_t d1=0x01;  uint8_t d2=0x02;  

I want to combine them as uint16_t as

uint16_t wd = 0x0201; 

How can I do it?

like image 810
johan Avatar asked Mar 06 '13 14:03

johan


People also ask

What is uint8_t and uint16_t?

So uint8_t is the same as an 8 bit unsigned byte. The uint16_t would be the same as unsigned int on an UNO. But it's half the size of unsigned int on a Due. The point of those is that you always know exactly how big they are.

What does uint16_t mean?

uint16_t is unsigned 16-bit integer. unsigned short int is unsigned short integer, but the size is implementation dependent. The standard only says it's at least 16-bit (i.e, minimum value of UINT_MAX is 65535 ). In practice, it usually is 16-bit, but you can't take that as guaranteed.


1 Answers

You can use bitwise operators:

uint16_t wd = ((uint16_t)d2 << 8) | d1; 

Because:

 (0x0002 << 8) | 0x01 = 0x0200 | 0x0001 = 0x0201 
like image 78
md5 Avatar answered Sep 19 '22 07:09

md5