Java 基本数据类型 && 位运算

时间:2023-03-09 03:57:18
Java 基本数据类型 && 位运算

1. Java基本数据类型

  1.1 数据类型示意图

类型 字节数 范围
byte 1 -128~127
short 2 -32768~32767
int 4

-231~231-1

long 8 -263~263-1
float 4  
double 8  
bolean 1  
char 2  

  (ps:  byte、char、short在运算时会自动提升到 int 类型)

  1.2 隐式转换&显式转换

Java 基本数据类型 && 位运算

  隐式类型转换:从存储范围小的类型到存储范围大的类型转换。

  显示类型转换:强制类型转换,从存储范围大的类型到存储范围小的类型转换。


2. 位运算

  2.1 位运算符

运算符 说明
&
|
^ 异或
~ 取反
<< 左移
>> 右移
>>> 无符号右移

  【&&与&的区别】

  &&存在短路,&通过位运算完成,无短路操作

  2.2 原码、反码、补码

  【原码】

  符号位加上数字的二进制表示,第一位为符号位(0为正数,1为负数)

  byte 类型的 +7 和 -7 的原码如下

  +7:00000111

  -7: 10000111

  【反码】

  正数的反码与原码相同,负数的反码符号位不变,其余位数取反。

  byte 类型的 +7 和 -7 的反码如下

  +7:00000111

  -7: 11111000

  【补码】

  正数的原码、反码、补码相同,负数的反码为原码取反加1

  byte 类型的 +7 和 -7 的补码如下

  +7:00000111

  -7: 11111001

  2.3 把 long 数据转换成字节数组相互转换

 /**
* 1. 把long数据转换成字节数组
* 2. 把字节数组数据转换成long
*/
public class TransDataType {
//把long数据转换成字节数组
public static byte[] long2Bytes(long lon) {
byte[] bytes = new byte[8];
for (int i = 0; i < 8; i++) {
bytes[i] = (byte) (lon >> (56 - 8 * i));
}
return bytes;
} //把字节数组数据转换成long
public static long bytes2Long(byte[] bytes) {
long val = 0;
for (int i = 0; i < 8; i++) {
long lon = ((long) bytes[i] & 0xff) << (56 - 8 * i);
val += lon;
}
return val;
} public static void main(String[] args) {
byte[] bytes = long2Bytes(-1275555);
for (int i = 0; i < bytes.length; i++) {
System.out.println(bytes[i]);
} System.out.println("=============================================="); long l = bytes2Long(bytes);
System.out.println(l);
}
}

  2.4 将 byte 变换成无符号的整数(0 ~ 255 , 正数不变)

 /**
* 将byte变换成无符号的整数(0 ~ 255 , 正数不变)
*/
public class TransByte2Int {
public static int bytes2Int(byte by){
int a = by&0xff;
return a;
} public static void main(String[] args) {
int i = bytes2Int((byte) -128);
System.out.println(i);
}
}

  2.5 定义函数,取出整数内存中的存储形态对应的16进制字符串与2进制字符串

 /**
* 定义函数,取出整数内存中的存储形态对应的16进制字符串
* 定义函数,取出整数内存中的存储形态对应的2进制字符串
*/
public class TransInt2String {
//定义函数,取出整数内存中的存储形态对应的16进制字符串
public static String int2HexString(int in) { StringBuilder builder = new StringBuilder();
char[] chars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
for (int i = 0; i < 8; i++) {
char c = chars[in >> (i * 4) & 0x0f];
builder.insert(0, c);
}
return builder.toString();
} //定义函数,取出整数内存中的存储形态对应的2进制字符串
public static String int2BinString(int in) { StringBuilder builder = new StringBuilder();
char[] chars = {'0', '1'};
byte[] bytes = new byte[8];
for (int i = 0; i < 32; i++) {
builder.append(chars[in >> (31 - i) & 0x01]);
}
return builder.toString();
} public static void main(String[] args) { System.out.println("==============================================");
String s1 = int2HexString(100);
System.out.println(s1); System.out.println("==============================================");
String s2 = int2BinString(100);
System.out.println(s2);
}
}

  2.6 有5亿整数(非负),去重计算不同整数的个数,300M内存

 package com.share.java.test;

 import org.junit.Test;

 /**
* 有5亿整数(非负),去重计算不同整数的个数,300M内存
* int的最大值为Integer.MAX_VALUE (2147483647/8/1024/1024=255)
* 一个byte有8位,定义长度为 Integer.MAX_VALUE/8 +1 的byte数组
* 每一位都表示一个数
* 遍历int数组
* 通过 整数/8 确定行,用过 整数%8 确定列
* 通过得到的行数和列数确定的位置,判断该位置是否存在1
* 若不存在,将该位置设置为1,count加1
*/
public class TestCalc { @Test
public void testCountUnique() {
int[] arr = {0, 1, 2, 3, 4, 1, 2, Integer.MAX_VALUE};
System.out.println(countUnique(arr));
} /**
* 去重统计整数个数
*/
public static int countUnique(int[] arr) {
//计算行数
int rows = Integer.MAX_VALUE / 8 + 1; //初始化字节数组
byte[] bytes = new byte[rows]; //计数器
int count = 0;
//
for (int i : arr) {
//定位行数
int row = i / 8;
//定位列数
int col = i % 8;
//
int r = (byte) (bytes[row] & (1 << col));
if (r == 0) {
count++;
bytes[row] = (byte) (bytes[row] | (1 << col));
}
}
return count;
}
}