Here you can find the source of setBitIfUnsetByIndex(final AtomicInteger ai, final int n)
Parameter | Description |
---|---|
ai | the AtomicInteger to operate on |
n | the nth bit from the least significant bit to compare |
public static boolean setBitIfUnsetByIndex(final AtomicInteger ai, final int n)
//package com.java2s; //License from project: Open Source License import java.util.concurrent.atomic.AtomicInteger; public class Main { /**//from w ww .jav a 2 s . com * @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; } }