If you ever had to work with controllers or microprocessors, you’ve probably come across the need to convert 2 cells of 16 bits numbers into a single 32-bit integer. On some controllers, the registers are assigned in 16-bit cells, and if you want to store anything larger than 65,535 you’ll need to convert it.
In this quick tip you can see a C# function for doing exactly that:
|
1 2 3 4 5 |
public static int GetIntFromBits(short Msb, short Lsb) int varInt = 0; varInt = varInt | Lsb; int intNum = BitConverter.ToInt32(byteArray, 0); } |
Even though this code is given in C#, it is relevant for other languages as well. The idea is to shift the most significant bits left, and then performing a bitwise OR on the least significant bits.
I’ll add other functions like that in the future.