运算符 | 运算 | 范例 |
---|---|---|
<< | 左移 | 3 << 2 = 12 --> 3*2*2=12 |
>> | 右移 | 3 >> 1 = 1 --> 3/2=1 |
>>> | 无符号右移 | 3 >>> 1 = 1 --> 3/2=1 |
& | 与运算 | 6 & 3 = 2 |
| | 或运算 | 6 | 3 = 7 |
^ | 异或运算 | 6 ^ 3 = 5 |
~ | 反码 | ~6 = -7 |
1.任意进制转十进制
1.转为int型:
public static int parseInt(String s,int radix)throws NumberFormatException
System.out.println(Integer.parseInt("1111",2));//15
2.转为Integer型:
public static Integer valueOf(String s,int radix) throws NumberFormatException
2.十进制转二进制
public static String toBinaryString(int i)
System.out.println(Integer.toBinaryString(15));//"1111"
3.十进制转十六进制
public static String toHexString(int i)
System.out.println(Integer.toHexString(15));//"f"
4.十进制转任意进制
public static String toString(int i, int radix)
System.out.println(Integer.toString(10,3));//101
1.交换律:可任意交换运算因子的位置,结果不变
2.结合律:即(a^b)^c==a^(b^c)
3.对于任何数x,都有x^x=0,x^0=x,同自己求异或为0,同0求异或为自己
4.自反性A^B^B=A^0=A,连续和同一个因子做异或运算,最终结果为自己
public static void main(String[] args) {
int num[] = new int[1001];
for(int i =0;i<1000;i++) {
num[i]=i+1;
}
num[1000]=111;
doubleNum(num);
}
public static void doubleNum(int num[]) {
int a = 0;
for(int i = 1;i<=num.length-1;i++) {
a=a^i;
}
for(int i = 0;i<num.length;i++) {
a=a^num[i];
}
System.out.println(a);
}
public static void main(String[] args) {
doubleNum(new int[]{
1,1,2,2,3,3,4,5,5});
}
public static void doubleNum(int num[]) {
int a = 0;
for(int i = 0;i<num.length;i++) {
a = a^num[i];
}
System.out.println(a);
}
public static void main(String[] args) {
countOne(15);
}
public static void countOne(int x) {
int count = 0;
//法一:
for(int i =0;i<32;i++) {
if((x&(1<<i))==1<<i) count++;
}
System.out.println(count);
count=0;
//法二:
for(int i=0;i<32;i++) {
if(((x>>>i)&1)==1) count++;
}
System.out.println(count);
count=0;
//法三:
while(x!=0) {
x=(x&(x-1));
count++;
}
System.out.println(count);
}
public static void main(String[] args) {
System.out.println(jiou(16));
}
public static int jiou(int x) {
int ou = x&0b10101010_10101010_10101010_10101010;
int ji = x&0b01010101_01010101_01010101_01010101;
return (ou>>1)+(ji<<1);
}
public static void main(String[] args) {
double num = 0.375;
StringBuilder sb = new StringBuilder();
sb.append("0.");
while(num!=0) {
num = num*2;
if(num>=1) {
sb.append("1");
num = num-1;
}else {
sb.append("0");
}
if(sb.length()>34) {
System.out.println("ERROR");
return;
}
}
System.out.println(sb);
}
思路:K个数进行不进位相加,最后在其K进制下各个数位为0
public static void main(String[] args) {
int arr[] = {
2,2,2,9,7,7,7,3,3,3,6,6,6,0,0,0};
char kRadix[][] = new char[arr.length][];
int maxlen = 0;
int k = 3;
for (int i = 0; i < arr.length; i++) {
kRadix[i]=new StringBuilder(Integer.toString(arr[i], k)).toString().toCharArray();
if(maxlen<kRadix[i].length) maxlen=kRadix[i].length;
}
int s[] = new int[maxlen];
for(int j =0;j<arr.length;j++) {
for(int i=0;i<maxlen;i++) {
if(i>=kRadix[j].length) break;
else s[i]+=kRadix[j][i]-'0';
}
}
StringBuilder sb = new StringBuilder();
for(int i=0;i<s.length;i++) {
sb.append(s[i]%k);
}
System.out.println(sb);
System.out.println(Integer.parseInt(sb.toString(), k));
}