1 /*
2  * Copyright (c) 2001, 2004, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  *
23  */
24 
25 package sun.jvm.hotspot.utilities;
26 
27 /** Bit manipulation routines */
28 
29 public class Bits {
30   public static final int AllBits = ~0;
31   public static final int NoBits  = 0;
32   public static final int OneBit  = 1;
33 
34   public static final int BitsPerByte =  8;
35   public static final int BitsPerInt  = 32;
36 
37   public static final int LogBytesPerInt  = 2;
38   public static final int LogBytesPerLong = 3;
39 
40 
setBits(int x, int m)41   public static int setBits(int x, int m) {
42     return x | m;
43   }
44 
clearBits(int x, int m)45   public static int clearBits(int x, int m) {
46     return x & ~m;
47   }
48 
nthBit(int n)49   public static int nthBit(int n) {
50     return (n > 32) ? 0 : (OneBit << n);
51   }
52 
setNthBit(int x, int n)53   public static int setNthBit(int x, int n) {
54     return setBits(x, nthBit(n));
55   }
56 
clearNthBit(int x, int n)57   public static int clearNthBit(int x, int n) {
58     return clearBits(x, nthBit(n));
59   }
60 
isSetNthBit(int word, int n)61   public static boolean isSetNthBit(int word, int n) {
62     return maskBits(word, nthBit(n)) != NoBits;
63   }
64 
rightNBits(int n)65   public static int rightNBits(int n) {
66     return (nthBit(n) - 1);
67   }
68 
maskBits(int x, int m)69   public static int maskBits(int x, int m) {
70     return x & m;
71   }
72 
maskBitsLong(long x, long m)73   public static long maskBitsLong(long x, long m) {
74     return x & m;
75   }
76 
77   /** Returns integer round-up to the nearest multiple of s (s must be
78       a power of two) */
roundTo(int x, int s)79   public static int roundTo(int x, int s) {
80     int m = s - 1;
81     return maskBits(x + m, ~m);
82   }
83 }
84