RT,在此,简单描述下Java中整数数据的表示方法
一:二进制、八进制、十六进制、UTF-8字符的表示方法
RT,在此,简单描述下Java中整数数据的表示方法一:二进制、八进制、十六进制、UTF-8字符的表示方法 //Binary 0b开头 int i = 0b110100010010101010; System.out.println("二进制数 0b1101:" + i); //Octets 0开头 int i01 = 016; System.out.println("八进制数 016:" + i01); //Hexdecimal 0x开头 int i02 = 0x124F; System.out.println("十六进制数 0x124F:" + i02); //utf-8 char c = '\u2122'; System.out.println("char:" + c); //十六进制的负数表示 -128 byte b = -0x80; System.out.println(b); //127 b = 0x7F; System.out.println(b);
二:移位操作
int i = ((12 & (1 << 3)) >>> 3); System.out.println(i); i = ((224 & (1 << 5)) >>> 5); System.out.println(i); i = ((224 & (1 << 4)) >>> 4); System.out.println(i);
三:对整数进行位运算
int i = 0B10010010; System.out.println("二进制数 0b10010010:" + i); //024在二进制中值为0B00010100 int i01 = 024; System.out.println("八进制数 024:" + i01); //与运算结果为0b10000 int i02 = i & i01; System.out.println("0b10010010 & 024:" + i02); //或运算结果为0b10010110 int i03 = i | i01; System.out.println("0b10010010 | 024:" + i03); //非运算结果为0b10000110 int i04 = i ^ i01; System.out.println("0b10010010 ^ 024:" + i04);
四:获取二进制数第i位的值
int i = 0B1101; //二进制表示为0B1101 //0B1101 & 0001 int i00 = (i & 1) / 1; //0B1101 & 0010 int i01 = (i & 2) / 2; //0B1101 & 0100 int i02 = (i & 4) / 4; //0B101 & 1000 int i03 = (i & 8) / 8; System.out.println("0位值:" + i00); System.out.println("1位值:" + i01); System.out.println("2位值:" + i02); System.out.println("3位值:" + i03);