zoukankan      html  css  js  c++  java
  • Java: 将指定的某一bit位 置0、置1、取反

    将指定的某一个比特位置0、置1、取反:

    /**
         * Set the specified bit to 1
         *
         * @param originByte Raw byte value
         * @param bitIndex   bit index (From 0~7)
         * @return Final byte value
         */
        public static byte setSpecifiedBitTo1(byte originByte, int bitIndex) {
            return originByte |= (1 << bitIndex);
        }
    
        /**
         * Set the specified bit to 0
         *
         * @param originByte Raw byte value
         * @param bitIndex   bit index (From 0~7)
         * @return Final byte value
         */
        public static byte setSpecifiedBitTo0(byte originByte, int bitIndex) {
            return originByte &= ~(1 << bitIndex);
        }
    
        /**
         * Invert the specified bit
         *
         * @param originByte Raw byte value
         * @param bitIndex   bit index (From 0~7)
         * @return Final byte value
         */
        public static byte setSpecifiedBitToReverse(byte originByte, int bitIndex) {
            return originByte ^= (1 << bitIndex);
        }
    
        /**
         * Get the value of the specified bit
         *
         * @param originByte Raw byte value
         * @param bitIndex   bit index (From 0~7)
         * @return Final byte value
         */
        public static byte getSpecifiedBitValue(byte originByte, int bitIndex) {
            return (byte) ((originByte) >> (bitIndex) & 1);
        }
  • 相关阅读:
    CCF CSP 201503-1 图像旋转
    CCF CSP 201403-1 相反数
    CCF CSP 201312-1 出现次数最多的数
    CCF CSP 201703-3 Markdown
    CCF CSP 201709-3 JSON查询
    CCF CSP 201709-2 公共钥匙盒
    CCF CSP 201709-1 打酱油
    CCF CSP 201604-4 游戏
    CCF CSP 201604-1 折点计数
    CCF CSP 201612-1 中间数
  • 原文地址:https://www.cnblogs.com/yongdaimi/p/13214479.html
Copyright © 2011-2022 走看看