Ok i dont really understand how this loop works, its the logic statement that bugs me the most. permissions is a constant value which I have assigned 127. Roles.java contains constant values that determine what roles can access certain pages of a website. Trouble is the logic statement returns true when bitmask is equal to one. How is this possible?
for (int bitMask = 1; bitMask <= 0x8000; bitMask *= 2)
{
boolean hasBit = (permissions & bitMask) != 0;
if (hasBit)
{
String role = Roles.getRole(bitMask);
if (role != null)
{
//Do stuff
}
else
{
//No role assigned
}
}
The binary equivalent of each of these numbers
The AND operator would return bits that are set in both
permissionANDbitMask. So the resuilt iswhich is
!= 0It is possible that
permissionsshould be128, becauseWhich would result in the zero you are expecting.