Java AtomicInteger setBitByValue(final AtomicInteger ai, final int value)

Here you can find the source of setBitByValue(final AtomicInteger ai, final int value)

Description

set Bit By Value

License

Open Source License

Parameter

Parameter Description
ai the AtomicInteger to operate on
value from the value get the index of the set bit from the least significant bit. Do the operation on that index.

Declaration

public static void setBitByValue(final AtomicInteger ai, final int value) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.concurrent.atomic.AtomicInteger;

public class Main {
    /**/*w  w w  . j  a va 2s  .  c om*/
     * @param ai the AtomicInteger to operate on
     * @param value from the value get the index of the set bit from the least significant bit. Do the operation on that
     *          index.
     * */
    public static void setBitByValue(final AtomicInteger ai, final int value) {
        setBitByIndex(ai, Integer.numberOfTrailingZeros(value));
    }

    /**
     * @param ai the AtomicInteger to operate on
     * @param n the nth bit from the least significant bit to compare
     * */
    public static void setBitByIndex(final AtomicInteger ai, final int n) {
        setBitIfUnsetByIndex(ai, n);
    }

    /**
     * @return true if operation is conducted, false if the bit is already set.
     * @param ai the AtomicInteger to operate on
     * @param n the nth bit from the least significant bit to compare
     * */
    public static boolean setBitIfUnsetByIndex(final AtomicInteger ai, final int n) {
        if (n >= Integer.SIZE) {
            throw new IllegalArgumentException("Out of int bit index boundary (31)");
        }
        int bitInt = 1 << n;
        int oldValue = ai.get();
        int newValue = oldValue | bitInt;
        while (newValue != oldValue && !ai.compareAndSet(oldValue, newValue)) {
            oldValue = ai.get();
            newValue = oldValue | bitInt;
        }
        return newValue != oldValue;
    }
}

Related

  1. reset()
  2. resetCounter()
  3. resetCounters()
  4. resetExceptionCount()
  5. setBit(AtomicInteger i, int mask)
  6. setBitIfUnsetByIndex(final AtomicInteger ai, final int n)
  7. storeStoreBarrier()
  8. timeToId(long timeInMillis)
  9. tokenize(String string)