Skip to main content
added 389 characters in body
Source Link
Michel Keijzers
  • 13k
  • 7
  • 42
  • 59

You can use the bit shift and and operator, like:

uint32_t data = 0xFFFF0001;

The following line will shift all bits 15 places to the right, meaning the least significant 15 bits will be removed, and the other bits shifted to the right.

uint32_t dataLeft = data >> 15; 

11111111111111110000000000000001
>>>>>>>>>>>>>>>11111111111111110000000000000001
| Bit shift   || orig. bits    ||Removed bits |

For the other 15 bits, you can use the and operator, which just makes all 1's 0's for those bits not in the bit mask (0x7FFF).

uint16_t dataRight = data & 0x7FFF;

value:              11111111111111110000000000000001
bit mask (0x7FFF):  00000000000000000111111111111111
                    -------------------------------- & / AND
                    00000000000000000000000000000001 

You can use the bit shift and and operator, like:

uint32_t data = 0xFFFF0001;

The following line will shift all bits 15 places to the right, meaning the least significant 15 bits will be removed, and the other bits shifted to the right.

uint32_t dataLeft = data >> 15;

For the other 15 bits, you can use the and operator, which just makes all 1's 0's for those bits not in the bit mask (0x7FFF).

uint16_t dataRight = data & 0x7FFF;

You can use the bit shift and and operator, like:

uint32_t data = 0xFFFF0001;

The following line will shift all bits 15 places to the right, meaning the least significant 15 bits will be removed, and the other bits shifted to the right.

uint32_t dataLeft = data >> 15; 

11111111111111110000000000000001
>>>>>>>>>>>>>>>11111111111111110000000000000001
| Bit shift   || orig. bits    ||Removed bits |

For the other 15 bits, you can use the and operator, which just makes all 1's 0's for those bits not in the bit mask (0x7FFF).

uint16_t dataRight = data & 0x7FFF;

value:              11111111111111110000000000000001
bit mask (0x7FFF):  00000000000000000111111111111111
                    -------------------------------- & / AND
                    00000000000000000000000000000001 
Source Link
Michel Keijzers
  • 13k
  • 7
  • 42
  • 59

You can use the bit shift and and operator, like:

uint32_t data = 0xFFFF0001;

The following line will shift all bits 15 places to the right, meaning the least significant 15 bits will be removed, and the other bits shifted to the right.

uint32_t dataLeft = data >> 15;

For the other 15 bits, you can use the and operator, which just makes all 1's 0's for those bits not in the bit mask (0x7FFF).

uint16_t dataRight = data & 0x7FFF;