circular Bit Shift Right - Java Language Basics

Java examples for Language Basics:Bit

Description

circular Bit Shift Right

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte b = 2;
        int count = 2;
        System.out.println(circularBitShiftRight(b, count));
    }/*www . ja v  a2  s.co  m*/

    public static byte circularBitShiftRight(byte b, int count) {
        if (count < 0)
            count = count * -1;

        if (count > 7)
            count = count % 8;

        if (count == 0)
            return b;

        if (b >= 0)
            return (byte) ((b >>> count) | (b << Byte.SIZE - count));
        else {
            return (byte) ((0x7f & b) >>> count | b << Byte.SIZE - count | (0x40 >>> count - 1));
        }
    }
}

Related Tutorials