Bitmask: Find attributes with bitmask

108 views Asked by At

I know it is a stupid question, but I does not understand how to solve my problem.

All I have is an integer via my getInteger() method and I have to check 2 attributes (just true or false) of that integer. This integer can have a1 or a2 and b1 or b2. For example:

16  --> 00010000 

0   --> 00000000 

248 --> 11111000 

8   --> 00001000

I know exactly this 4 integers (there may be others I dont know at the moment) and the corresponding attributes:

16  means attribute a1 and b1
0   means attribute a1 and b2
248 means attribute a2 and b1
8   means attribute a2 and b2

So I think that:

Type A1 is sth like xxxx0xxx
Type A2 is sth like xxxx1xxx
Type B1 is sth like xxx1xxxx 
Type B2 is sth like xxx0xxxx 

Hope this is correct?

How can I check in java if an integer is from type attribut a1/a2 and b1/b2? I need sth like:

public boolean isA(int integer){
   //mask example: 0xf0
   int maskA = 0x??;
   System.out.println("isA is:" + ((integer & maskA) ? true : false))
   return (integer & maskA) ? true : false;
}

public boolean isB(){   
   int maskB = 0x??;
   ... similar as above ...
}

I need the correct Bitmask, but I also want to understand how to get it.

0

There are 0 answers