//不用乘法和加法给一个数增加7倍 public class DivideS { public static void main(String... args) { // int x = 8; int a = x << 2; int b = x << 1; int s = a^b^x; System.out.println(s); } }
i -= i<<3; System.out.println(-i);
(x << 3) - x
增加7倍就是变成8倍,左移3位即可: x = x << 3;
x = x < < 3;
果然, It's a trap!!!!!!
x = x << 3;最佳答案。我想没有更好的方法了吧。
//无符号不算最高位的加法,其它的也可以实现吧,减法等于加上它的补码,LZ要几倍都行public class Main { public static long sum(long a,long b){ long c=0; long bf1=0x0001,bf=0; byte jw=0; for(int i=0;i<Long.SIZE;i++){ long ai=a&(0x0001<<i); long bi=b&(0x0001<<i); if(ai>0){//1 //为了方便理解用if,不用?:运算 if(bi>0){//1 if(jw>0){//1 jw=1; bf|=0x0001<<i;//确定第i位为1,进1 }else{ jw=1;//确定第i位为0//进1 } }else{//0 if(jw>0){//1 jw=1;//确定第i位为0//进1 }else{ jw=0; bf|=0x0001<<i;//确定第i位为1,进0 } } }else{//0 if(bi>0){//1 if(jw>0){//1 jw=1;//确定第i位为0//进1 }else{ jw=0; bf|=0x0001<<i;//确定第i位为1,进0 } }else{//0 if(jw>0){//1 jw=0; bf|=0x0001<<i;//确定第i位为1,进0 }else{ jw=0;//确定第i位为0//进0 } } } } return bf; } public static void main(String[] args) throws FileNotFoundException { System.out.println(""+sum(20, 111)); } }
不用+运算实现加法 int Add(int a,int b) { int jw=a&b; int jg=a^b; while(jw) { int t_a=jg; int t_b=jw<<1;//进位 jw=t_a&t_b;//求进位 jg=t_a^t_b;//求相加的结果,不考虑进位 } return jg; }
x = (x << 3) - x;
//不用乘法和加法给一个数增加7倍
public class DivideS
{
public static void main(String... args) {
//
int x = 8;
int a = x << 2;
int b = x << 1;
int s = a^b^x;
System.out.println(s);
}
}
i -= i<<3;
System.out.println(-i);
x = x << 3;
It's a trap!!!!!!
public static long sum(long a,long b){
long c=0;
long bf1=0x0001,bf=0;
byte jw=0;
for(int i=0;i<Long.SIZE;i++){
long ai=a&(0x0001<<i);
long bi=b&(0x0001<<i);
if(ai>0){//1 //为了方便理解用if,不用?:运算
if(bi>0){//1
if(jw>0){//1
jw=1;
bf|=0x0001<<i;//确定第i位为1,进1
}else{
jw=1;//确定第i位为0//进1
}
}else{//0
if(jw>0){//1
jw=1;//确定第i位为0//进1
}else{
jw=0;
bf|=0x0001<<i;//确定第i位为1,进0
}
}
}else{//0
if(bi>0){//1
if(jw>0){//1
jw=1;//确定第i位为0//进1
}else{
jw=0;
bf|=0x0001<<i;//确定第i位为1,进0
}
}else{//0
if(jw>0){//1
jw=0;
bf|=0x0001<<i;//确定第i位为1,进0
}else{
jw=0;//确定第i位为0//进0
}
}
}
}
return bf;
}
public static void main(String[] args) throws FileNotFoundException {
System.out.println(""+sum(20, 111));
}
}
int Add(int a,int b)
{
int jw=a&b;
int jg=a^b;
while(jw)
{
int t_a=jg;
int t_b=jw<<1;//进位
jw=t_a&t_b;//求进位
jg=t_a^t_b;//求相加的结果,不考虑进位
}
return jg;
}
learning~~~~~~~~~~~~~`