Java Bit Set setBit(int i, int bit, boolean state)

Here you can find the source of setBit(int i, int bit, boolean state)

Description

set Bit

License

Open Source License

Parameter

Parameter Description
i a parameter
bit a parameter
state a parameter

Return

e.g. setBit(8, 0, 1) == 9

Declaration

public static int setBit(int i, int bit, boolean state) 

Method Source Code

//package com.java2s;
/* /*from  ww w.  j a  va  2 s . c  o m*/
    
 Created on Mar 4, 2005
    
 The Bungee View applet lets you search, browse, and data-mine an image collection.  
 Copyright (C) 2006  Mark Derthick
    
 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.  See gpl.html.
    
 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    
 You may also contact the author at 
 mad@cs.cmu.edu, 
 or at
 Mark Derthick
 Carnegie-Mellon University
 Human-Computer Interaction Institute
 Pittsburgh, PA 15213
    
 */

public class Main {
    /**
     * @param i
     * @param bit
     * @param state
     * @return e.g. setBit(8, 0, 1) == 9
     */
    public static int setBit(int i, int bit, boolean state) {
        int mask = 1 << bit;
        if (state) {
            i |= mask;
        } else {
            i &= ~mask;
        }
        return i;
    }
}

Related

  1. setBit(int data, int bit, int value)
  2. setBit(int f, int i)
  3. setBit(int flag, int i)
  4. setBit(int flags, int bit)
  5. setBit(int i, int bit, boolean enabled)
  6. setBit(int integer, int bit)
  7. setBit(int mask, int bit, boolean value)
  8. setBit(int n, int bitPosition)
  9. setBit(int position, boolean value, int meta)