circular Bit Shift Left - Java Language Basics

Java examples for Language Basics:Bit

Description

circular Bit Shift Left

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(circularBitShiftLeft(b, count));
    }//www.  ja va 2s  .co  m

    public static byte circularBitShiftLeft(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) ((b) << count | (0x7f & b) >>> Byte.SIZE - count | (0x40 >>> (Byte.SIZE
                    - count - 1)));
        }
    }
}

Related Tutorials