Hello I have a uint16_t number and I need to split it in 3 parts.
The first 5 bits of this number gives me the hour.
The next 6 bits of this number gives me the minutes.
The next 5 bits of this number gives me the seconds.
So I need to split it in those 3 parts to get the info I need.
any idea ?
Leaving aside that you can't split 8 bits into 16 (I'm going to assume you actually have a uint16_t, since that makese sense here), what you want to is called a mask. Let's say your 16 bit value is stored in variable "value"
To get the first (most significant) 5 bits, you'd use the mask 0xF800, which in binary is 1111100000000000.
You use a bitwise AND to apply this:
const uint16_t MASK;
uint16_t value = whatever_was_passed_in;
value = value & MASK;
This zeros out the bits you aren't interested in. If you want those bits shifted down to the lowest part of the variable, you can do that with a right shift:
value = (value & MASK) >> 11;
The other chuncks are handled with the same process, but different masks and shift values.
typedef struct bit_time {
unsigned int hour:5;
unsigned int minute:6;
unsigned int seconds:5;
} bit_time_t;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.