Clears a range of bits in the specified integer. : Binary Bit « Language Basics « Java






Clears a range of bits in the specified integer.

     
/***
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *    
 *    Linking this library statically or dynamically with other modules 
 *    is making a combined work based on this library. Thus, the terms and
 *    conditions of the GNU General Public License cover the whole
 *    combination.
 *    
 *    As a special exception, the copyright holders of this library give 
 *    you permission to link this library with independent modules to 
 *    produce an executable, regardless of the license terms of these 
 *    independent modules, and to copy and distribute the resulting 
 *    executable under terms of your choice, provided that you also meet, 
 *    for each linked independent module, the terms and conditions of the 
 *    license of that module.  An independent module is a module which 
 *    is not derived from or based on this library.  If you modify this 
 *    library, you may extend this exception to your version of the 
 *    library, but you are not obligated to do so.  If you do not wish 
 *    to do so, delete this exception statement from your version.
 *
 *    Project: www.simpledbm.org
 *    Author : Dibyendu Majumdar
 *    Email  : d dot majumdar at gmail dot com ignore
 */
//package org.simpledbm.common.util;

/**
 * @author Dibyendu
 * @since 20 Nov 2006
 */
public class BitwiseOperations {

  /**
   * Clears a range of bits in the specified integer.
   * 
   * @param x
   *            Integer argument
   * @param p
   *            Position (0-31)
   * @param n
   *            Number of bits to clear
   * @return Modified integer
   */
  static final int clearbits(final int x, final int p, final int n) {
    return x & ~((~0 << (32 - n)) >>> p);
  }

}

   
    
    
    
    
  








Related examples in the same category

1. Utility for byte swapping of all java data types
2.Bitwise DemoBitwise Demo
3.Binary Digits
4.Using the bitwise operatorsUsing the bitwise operators
5.Bitwise complement (~): inverts ones and zeroes in a number
6.Convert a number to negative and back
7.Bitwise AND (&)
8.Bitwise OR (|)
9.Bitwise XOR (^)
10.Left shift (<<)
11.Performing Bitwise Operations on a Bit Vector
12.Converting Between a BitSet and a Byte Array
13.Returns a byte array of at least length 1
14.Shift to the left
15.Signed shift to the right
16.Unsigned shift to the right
17.Bit-level unpacking of floating-point data
18.Gets the a single bit of the target.
19.Returns 16 bits from the long number.
20.Sets a specific bit of an int.
21.Fuses the lower 16 bits of two ints.
22.Class to represent unsigned 32-bit numbers.
23.A list of bits.
24.Gets the specified bit (0-31) from the integer argument.
25.Sets the specified bit (0-31) in the integer argument.