OSDN Git Service

884ab502dc9c6bd7fb230138566f57283835ac4d
[pf3gnuchains/gcc-fork.git] / libjava / gnu / gcj / awt / BitMaskExtent.java
1 /* Copyright (C) 2000  Free Software Foundation
2
3    This file is part of libgcj.
4
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
7 details.  */
8
9 package gnu.gcj.awt;
10
11 /** 
12  * Simple transparent utility class that can be used to perform bit
13  * mask extent calculations.
14  */
15 public final class BitMaskExtent
16 {
17   /** The number of the least significant bit of the bit mask extent. */
18   public byte leastSignificantBit;
19
20   /** The number of bits in the bit mask extent. */
21   public byte bitWidth;
22   
23   /**
24    * Set the bit mask. This will calculate and set the leastSignificantBit
25    * and bitWidth fields.
26    *
27    * @see #leastSignificantBit
28    * @see #bitWidth
29    */
30   public void setMask(long mask)
31   {
32     leastSignificantBit = 0;
33     bitWidth = 0;
34     if (mask == 0) return;
35     long shiftMask = mask;
36     for (; (shiftMask&1) == 0; shiftMask >>>=1) leastSignificantBit++;
37     for (; (shiftMask&1) != 0; shiftMask >>>=1) bitWidth++;
38     
39     if (shiftMask != 0)
40       throw new IllegalArgumentException("mask must be continuous");
41   }
42   
43   /** 
44    * Calculate the bit mask based on the values of the
45    * leastSignificantBit and bitWidth fields.
46    */
47   public long toMask()
48   {
49     return ((1<<bitWidth)-1) << leastSignificantBit;
50   }  
51 }