I've come across a code where it uses bitwise operator. I dont understand it well enough. What im trying to do is to save a truck size id but when reading the values, the ID im getting is different.
CODE FOR SAVING
public int getmergedfields()
{
int merged = (int)PriorityLevel;
merged |= ((int)PriceSource << 4);
merged |= (((int)BinWeight) << 8);
// merged |= (((int)WhenToInvoice) << 10);
// these two flags store whether this job has extended comments for
// either the comments or the officenotes field
if (comments.Length > 200)
merged += 1024; // set the 11th bit
if (officenotes.Length > 200)
merged += 2048; // set the 12th bit
if (TLPricedPerCube)
merged += 4096;
if (SentToMobileDevice)
merged += 8192;
// before this property was here things were always saved as ExGST so make that the default
if (TLPricedIncGST)
merged += 16384;
if (DespatchDocketSent)
merged += 32768;
if (IsCurrentJob)
merged += 65536;
merged += ((int)TruckSize) << 17;
return merged;
}
CODE FOR READING
int unmerge = JobReader.GetInt32(startat);
PriorityLevel = (ePriorityLevel) (unmerge & 15); // 0x1111); // priority stored in 4 low bits
PriceSource = (ePriceSource)((unmerge >> 4) & 15);
BinWeight = (eBinWeight) ((unmerge >> 8) & 3);
// whentoinvoice = (eWhenToInvoice)((unmerge >> 10) & 3);
if ((unmerge & 1024) > 0)
extendedcomments = true;
if ((unmerge & 2048) > 0)
extendedofficenotes = true;
if ((unmerge & 4096) > 0)
TLPricedPerCube = true;
if ((unmerge & 8192) > 0)
senttomobiledevice = true;
if ((unmerge & 16384) > 0)
TLPricedIncGST = true;
if ((unmerge & 32768) > 0)
DespatchDocketSent = true;
if ((unmerge & 65536) > 0)
IsCurrentJob = true;
TruckSize = ((unmerge & ((1 << 17) | (1 << 18))) >> 17);
If the truck size Id is less than or equal to three, it works just fine but when the ID is greater than three, the result im expecting is different.
Honestly, I dont know what this code means.
TruckSize = ((unmerge & ((1 << 17) | (1 << 18))) >> 17);
merged += ((int)TruckSize) << 17;
I don't know where does the 17 and 18 come from.