BitOperation

package a;

/**
 * 
 * @author ZengWenFeng
 */
public class BitOperation
{
	public static boolean com01()
	{
		System.out.println("com01()");
		return false;
	}
	
	public static boolean com02()
	{
		System.out.println("com02()");
		return true;
	}
	
	public static boolean com03()
	{
		System.out.println("com03()");
		return true;
	}

	public static void main(String[] args)
	{
		if (com01() | com02() | com03() == false)
		{
			System.out.println("test |");
		}

		System.out.println("---------------------");
		
		if (com01() || com02() || com03() == false)
		{
			System.out.println("test ||");
		}
		
		System.out.println("------------");
		System.out.println(Integer.toBinaryString(2));//0000 0001
		System.out.println(Integer.toBinaryString(2));//0000 0010
		System.out.println(Integer.toBinaryString(3));//0000 0011
		System.out.println(Integer.toBinaryString(4));//0000 0100
		System.out.println(Integer.toBinaryString(5));//0000 0101
		System.out.println(Integer.toBinaryString(6));//0000 0110
		
		//32bit
		System.out.println(Integer.toBinaryString(3)); //0000 0000 0000 0000 0000 0000 0000 0011
		System.out.println(Integer.toBinaryString(-4));//1111 1111 1111 1111 1111 1111 1111 1100
		
		System.out.println("------------");
		System.out.println(2 | 3);
		System.out.println(2 & 3);
		System.out.println(4 & 3);
		System.out.println(~3);//-4
		
		//0000 0101
		//0000 0110
		//相同为0,不同为1
		
		//0000 0011
		
		// 1 * 2^1 + 1 * 2^0
		System.out.println(5 ^ 6);//3
		System.out.println("------------");
		
		//0000 0101  >> 
		//0000 0001
		//5 / 2^2 = 5 / 4 = 1
		long x1 = System.nanoTime();
		System.out.println("5 >> 2  : " + (5 >> 2));
		long x2 = System.nanoTime();
		System.out.println(x2 - x1);//14929
		
		long x3 = System.nanoTime();
		System.out.println("5 / 4  : " + (5 / 4));
		long x4 = System.nanoTime();
		System.out.println(x4 - x3);//13529
		
		//0000 0101  <<
		//0001 0100
		//5 * 2^3 = 5 * 8 = 40
		System.out.println("-5 << 3  : " + (-5 << 3));
		
		System.out.println(Integer.toBinaryString(-9));//11111111111111111111111111110111
		//1111 1111 1111 1111 1111 1111 1111 0111
		//   1 1111 1111 1111 1111 1111 1111 1110
		//000
		//0001 1111 1111 1111 1111 1111 1111 1110
		System.out.println("9 >>> 3  : " + (-9 >>> 3));//536870910
		
		x3 = System.nanoTime();
		String str = Integer.valueOf("0001 1111 1111 1111 1111 1111 1111 1110".replaceAll(" ", ""), 2).toString();
//		str = Integer.parseInt("", 2);
		System.out.println(str);
		x4 = System.nanoTime();
		System.out.println(x4 - x3);//1341716
		
		x3 = System.nanoTime();
		int i = Integer.parseInt("0001 1111 1111 1111 1111 1111 1111 1110".replaceAll(" ", ""), 2);
		System.out.println(i);
		x4 = System.nanoTime();
		System.out.println(x4 - x3);//70445
		
		/*
		com01()
		com02()
		com03()
		test |
		---------------------
		com01()
		com02()
		test ||
		 */
	}
}


   public static String toOctalString(int i)
    {
        return toUnsignedString(i, 3);
    }

    public static String toBinaryString(int i)
    {
        return toUnsignedString(i, 1);
    }

    private static String toUnsignedString(int i, int j)
    {
        char ac[] = new char[32];
        int k = 32;
        int l = 1 << j;
        int i1 = l - 1;
        do
        {
            ac[--k] = digits[i & i1];
            i >>>= j;
        } while(i != 0);
        return new String(ac, k, 32 - k);
    }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值