Wonderful-life217

主要内容

1.十进制二进制互转

2.二进制的位运算

3.JDK内置的进制转换

4.JAVA中的进制

十进制二进制互转

57 111001

 

 

二进制的位运算:优点:特定情况下,计算方便,被支持面广泛。

① 按位与& (两位全位1,结果才为1)
0与0=0;0与1=0;1与0=0; 1与1=1;
例:51与5
00110011
---------------
00000101
=00000001
=1
位运算的特殊用法:
*清零,取一个数中的指定位
 
②按位或 | (只要有一个位位1,结果就为1)
0|0=0; 0|1=1; 1|0=1; 1|1=1;
例:51|5 =
00110011
---------------
00000101
=00110111 = 55
或运算的特殊用法:
常用来对一个数据的某些位置1;
 
③异或运算 ∧ (两个相应位为“异”,则该结果为1,否则为0)
0∧ 0=0; 0∧ 1=1; 1∧ 0=1; 1∧ 1=0;
例:51∧ 5=
00110011
---------------
00000101
=00110110=54
特殊用法:
*使特定位翻转
*与0异或保留原值
*取反运算(对一个二进制数按位取反,即将0变为1,1变为0)
 
利用与0异或,两个变量交换值的方法:
两个变量交换值的方法有:
1.借助第三个变量
C=A; A=B; B=C;
2.利用加减法
C=A+B; B=C-B; A=C-B;
3.用位异或运算实现 (效率最高)
A=A∧B; B =A∧B; A=A∧B;
 
左移运算<< (将一个运算对象的各二进制位全部左移若干位,左边的二进制位丢弃,右边补0)
2<<1= 4
 
右移运算>>(将一个运算对象的各二进制位全部右移若干位,正数左补0,负数左补1,右边丢弃)
 
无符号右移
 
负数以其正值的补码形式表示
 
原码:一个正数按照绝对值大小转换成的二进制称为原码。
 
反码
 
补码:反码加1称为补码
 

JDK内置的进制转换:

 

JAVA中的进制:

平时开发,进制转换和位操作用的并不多,因为JAVA处理的是高层
在跨平台中用的较多,文件读写,数据通信等。
 
基本数据类型:
int数据类: 1字节= 8bit
byte(8bit,-128~127)
short(16bit)
int(32bit)
long(64bit)
float:单精度32bit ,双精度64bit
bolean:true 1bit,false 1bit
char:unicode字符 16位
对应的包装类:Integer......
 
在JAVA里面除去基本数据类型的其它类型都是引用数据类型,String是一个类,所以String不是基本类型而是引用类型。

数据类型转换为字节:
int (8143)
8143(00000000,00000000,00011111,11001111)=byte[]={-49,31,0,0}
第一个字节(低端) 8143>>0*8&0xff=(11001111)=207或有符号-49
第二个字节(低端) 8143>>1*8&0xff=(00001111)=31
第三个字节(低端) 8143>>2*8&0xff=(00000000)=0
第四个字节(低端) 8143>>3*8&0xff=(00000000)=0
 
byte转int的时候为什么非要先&0xff计算出来才是正确答案?
首先,JAVA中的二进制采用的是补码形式,并非原码或反码,这3个概念要搞清楚;
其次,byte占8位,int占32位,将byte强制转换为int型时,如果没有做 & 0xff运算,且byte对应的值为负数的话,就会对高位3个字节进行补位,这样就有可能出现补位误差的错误。
举例来说,byte型的-1,其二进制(补码)为11111111(即0xff),转换成int型,值也应该为-1,但经过补位后,得到的二进制为11111111111111111111111111111111(即0xffffffff),这就不是-1了,对吧?
而0xff默认是int型,所以,一个byte跟0xff相与,会先将那个byte转化成int型运算,这样,结果中的高位3个字节就总会被清0,于是结果就是我们想要的了~
 
字符串转化为字节数据 :
String s; byte[] bs = s.getBytes();
字节数组转化为字符串:
String s = new Stirng(bs);
String s = new String(bs,encode);//encode指编码方式 "gb2312,utf-8"
 1 public class Convert {
 2 
 3     /**
 4      * <<左移运算<<右移运算<<<无符号右移
 5      */
 6 
 7     // int转Byte[]
 8     public static byte[] int2Bytes(int id) {
 9         byte[] arr = new byte[4];
10         arr[0] = (byte) ((int) (id >> 0 * 8) & 0xff);
11         arr[1] = (byte) ((int) (id >> 1 * 8) & 0xff);
12         arr[2] = (byte) ((int) (id >> 2 * 8) & 0xff);
13         arr[3] = (byte) ((int) (id >> 3 * 8) & 0xff);
14         return arr;
15     }
16 
17     // Byte[]转int
18     public static int Bytes2int(byte[] arr) {
19         int rs0 = (int) ((arr[0] & 0xff) << 0 * 8);
20         int rs1 = (int) ((arr[1] & 0xff) << 1 * 8);
21         int rs2 = (int) ((arr[2] & 0xff) << 2 * 8);
22         int rs3 = (int) ((arr[3] & 0xff) << 3 * 8);
23         return rs0 + rs1 + rs2 + rs3;
24     }
25 
26     // long转化为byte[]
27     public static byte[] long4Bytes(long id) {
28         byte[] arr = new byte[8];
29         for (int i = 0; i < arr.length; i++) {
30             arr[i] = (byte) ((int) (id >> i * 8) & 0xff);
31         }
32         return arr;
33     }
34 
35     /**
36      * byte[]转化为long型和转化int型一样,只是long是64bit,而int是32bit
37      */
38     public static void main(String[] args) {
39         byte[] arr = Convert.int2Bytes(8143);
40         System.out.println(arr[0] + "\n" + arr[1] + "\n" + arr[2] + "\n" + arr[3]);
41         int rs = Convert.Bytes2int(arr);
42         System.out.println(rs);
43 
44         byte[] arr2 = Convert.long4Bytes(20);
45         for (byte b : arr2) {
46             System.out.println(b);
47         }
48 
49         // 字符串与字节数组:
50         String describle = "我是字符串";
51         byte[] barr = describle.getBytes();
52         String des = new String(barr);
53         System.out.println(des);
54     }
55 
56 }
View Code

分类:

技术点:

相关文章: