位移工具

public class BitMoveUtil {

    /**
     * 把value的第index位设置为true or false <br/>
     * 如:0x1111 1111<br/>
     * 索引:7654 3210<br/>
     * @param value 目标
     * @param index 位数索引(从右往左的index+1位,右边第一位无视)
     * @param yes true or false
     * @return
     */
    public static int set(int value, int index, boolean yes){
        if( yes ){
            value |= (1<<index);
        }else {
            value &= ~(1<<index);
        }
        return value;
    }

    /**
     * 取出value的index位是true or false <br/>
     * 如:0x1111 1111<br/>
     * 索引:7654 3210<br/>
     * @param value 目标值
     * @param index 位数索引(从右往左的index+1位,右边第一位无视)
     * @return
     */
    public static boolean get(int value, int index){
        return  ( (value >> (index)) & 1 ) == 1;
    }

}

猜你喜欢

转载自blog.csdn.net/zhang168/article/details/80941077