This is a code i am seeing for some time.
i wonder how the code works.
public static final int MULTI = 1 << 1;
public static final int SINGLE = 1 << 2;
public static final int READ_ONLY = 1 << 3;
SWT.MULTI | SWT.SINGLE | SWT.READ_ONLY
i went deep into the class implementation of TableViewer(Composite parent, int style) in search for the answer but didnt find much.
I found this code, but didnt understand much
static int checkBits (int style, int int0, int int1, int int2, int int3, int int4, int int5) {
int mask = int0 | int1 | int2 | int3 | int4 | int5;
if ((style & mask) == 0) style |= int0;
if ((style & int0) != 0) style = (style & ~mask) | int0;
if ((style & int1) != 0) style = (style & ~mask) | int1;
if ((style & int2) != 0) style = (style & ~mask) | int2;
if ((style & int3) != 0) style = (style & ~mask) | int3;
if ((style & int4) != 0) style = (style & ~mask) | int4;
if ((style & int5) != 0) style = (style & ~mask) | int5;
return style;
}
Packing Bits Together
Let’s take a look at this code.
The “<<” operator means shift the bits to the left. The number after the “<<” operator tells us how many bits to shift.
So, another way of writing the code would be.
By defining the flags this way, the values can be OR’d (“added”) together and saved in one byte or one integer.
This is saying to OR the bits of the two values together. But what does that mean?
In this case, since we’ve defined our values as a single bit, it’s the same as adding the values.
Therefore, the status value is 6 (2 + 4).
Now remember, we’re not adding. Because we’re using bits, the effect is the same as if we’re adding.
Unpacking Bits
The second bit of code you’ve posted basically takes the 6 that we came up with, and splits it back into 2 and 4. It works by checking each bit, one at a time, and seeing if it’s present.
Let’s take one line, and see what it’s doing.
int1represents one of the style bits. For the sake of this discussion, let’s say that it is MULTI with a value of 2.The first part (the if condition) tests for whether or not the bit is set in the style integer.
The second part is a bit trickier. (Bit, get it. Very punny.)
The value of mask is given in the code.
This is just all of the status values OR’d together. From our original example of MULTI, SINGLE, and READ_ONLY, that gives us a mask of x’0E’ or 14. Through the rest of this discussion, I’ll be using hexadecimal.
Status Bit Present
Now, back to the line we’re talking about.
style & int1is an AND operation. That means the bit has to be set in bothstyleandint1.Styleis x’06’ (2 + 4) from when we set it before.int1is x’02’. When you AND x’06’ and x’02’, you get x’02’, which makes the value of the iftrue.~maskconverts the x’0E’ into x’F1′. In other words, all of the bits are flipped from 0 to 1 and 1 to 0.style & ~maskis an AND operation. When you AND x’06’ and x’F1′, you get x’00’. In other words, none of the bits match up.We said earlier that int1 is MULTI, with the value of 2 or x’02’. Now we do an OR operation, which we explained before. ORing x’00’ and x’02’ gives us x’02’, which is the MULTI value that we wanted to extract.
Status Bit Not Present
The other alternative (when the status bit is not present) leads to a slightly different result. We didn’t set READ_ONLY, so lets go through that calculation.
READ_ONLY is x’08’. Style is x’06’ When you AND those values together in the if
you get x’00’, which causes the value of the if to be
false.Justification
The original reason for putting several status values into one byte or word was to save memory. This was important 40 years ago when computer memory was limited. Now, it’s done for convenience in passing status around. Rather than passing 7 or 15 different status indicators from one method to another, you pass one status indicator.
The tradeoff, as you have seen, is that it takes a bit of code to extract the status bits so you can use them.