Reputation: 61
I am sending data to a connected device and have the issue come up where one integer retrunded from the device holds two id's. One is packed in the 10 LSB bits and the second is packed in the 22 MSB bits. From help on this site I found how to parse the two numbers ( i believe this is correct anyways):
//10 LSB bits
var nodeID = source & 0x3FF; //mask 10 LSBs
//22 MSB bits: mask + bitshift
var companyID = (source & 0x3FFFFF) >> 10;
Now i need to create an int32 from two numbers (10 bits and 22 bits respectively) and need some direction again. What would be the steps involved?
Thanks!
Upvotes: 1
Views: 922
Reputation: 2981
// MSB (Most significant bits); From left to right
// LSB (Least significant bits); From right to left
// 10 LSBits
var result = source1 & 0x3FF;
// 10 MSBits
var result = (source1 >> 22) & 0x3FF;
// 22 LSBits
var result = source2 & 0x3FFFFF;
// 22 MSBits
var result = (source2 >> 10) & 0x3FFFFF;
// 10 LSBits pack with 22 MSBits
var result = ((source1 & 0x3FF) << 22) | ((source2 >> 10) & 0x3FFFFF);
// 10 MSBits pack with 22 LSBits (1'st variation)
var result = (source1 & 0xFFC00000) | (source2 & 0x3FFFFF);
// 10 MSBits pack with 22 LSBits (2'nd variation)
var result = (((source1 >> 22) & 0x3FF) << 22) | (source2 & 0x3FFFFF);
// 10 LSBits pack with 22 LBSBits
var result = ((source1 & 0x3FF) << 22) | (source2 & 0x3FFFFF);
// 10 MSBits pack with 22 MSBits (1'st variation)
var result = (source1 & 0xFFC00000) | ((source2 >> 10) & 0x3FFFFF);
// 10 MSBits pack with 22 MSBits (2'nd variation)
var result = (((source1 >> 22) & 0x3FF) << 22) | ((source2 >> 10) & 0x3FFFFF);
// 22 LSBits pack with 10 MSBits
var result = ((source2 & 0x3FFFFF) << 10) | ((source1 >> 22) & 0x3FF);
// 22 MSBits pack with 10 LSBits (1'st variation)
var result = (source2 & 0xFFFFFC00) | (source1 & 0x3FF);
// 22 MSBits pack with 10 LSBits (2'nd variation)
var result = (((source2 >> 10) & 0x3FFFFF) << 10) | (source1 & 0x3FF);
// 22 LSBits pack with 10 LSBits
var result = ((source2 & 0x3FFFFF) << 10) | (source1 & 0x3FF);
// 22 MSBits pack with 10 MSBits (1'st variation)
var result = (source2 & 0xFFFFFC00) | ((source1 >> 22) & 0x3FF);
// 22 MSBits pack with 10 MSBits (2'nd variation)
var result = (((source2 >> 10) & 0x3FFFFF) << 10) | ((source1 >> 22) & 0x3FF);
Choose which one you like...
Upvotes: 0
Reputation: 74277
Doesn't seem like it should be more complicated than something like:
// convenience overload
public static int Pack( int x, int y )
{
return (int) Pack( (uint)x, (uint)y);
}
// convenience overload
public static void Unpack( int p, out int x, out int y )
{
uint x1, y1;
Unpack( (uint)p, out x1, out y1 );
x = (int) x1;
y = (int) y1;
}
// the guts of the pack operation
public static uint Pack( uint x , uint y )
{
if ( ( x & 0xFFC00000u ) != 0 ) throw new ArgumentOutOfRangeException("argument out of range", "x");
if ( ( y & 0xFFFFFC00 ) != 0 ) throw new ArgumentOutOfRangeException("argument out of range", "y");
uint z = ( x << 10 ) | ( y & 0x000003FF );
return z;
}
// the guts of the unpack operation
public static void Unpack( uint p, out uint x , out uint y )
{
x = ( p & 0xFFFFFC00u ) >> 10 ;
y = ( p & 0x000003FFu ) ;
}
Upvotes: 0
Reputation: 186718
Well it seems you have
companyID nodeID
<- 22 -> <-10->
format. If it's your case you can
var nodeID = source & 0x3FF; // Rightmost 10 bits: just mask
var companyID = (source >> 10) & 0x3FFFFF; // Move to the right, then mask
Please, note that for companyID
you should first shift it to the right in order to get rid of the leftmost 10
(nodeID
) bits:
Initial:
companyID nodeID
<- 22 -> <-10->
After Shift ( >> 10
):
sssssssss companyID
sign bits <- 22 ->
After Masking ( & 0x3FFFFF
):
00..00 companyID == companyID
<-10-> <- 22 ->
Reverse:
var encoded = (CompanyID << 10) | nodeID
Upvotes: 2