Java 没有'*';操作人员
在学习C语言的过程中,我刚刚复习了一些基本的知识。我遇到了一个问题,不使用*运算符就可以将一个数字乘以7。基本上是这样的Java 没有'*';操作人员,java,c++,c,bit-manipulation,Java,C++,C,Bit Manipulation,在学习C语言的过程中,我刚刚复习了一些基本的知识。我遇到了一个问题,不使用*运算符就可以将一个数字乘以7。基本上是这样的 (x << 3) - x; (x一个左移整数乘以2,前提是它不会溢出。只要在接近时进行适当的加或减即可。当归结到它时,可以像这样进行正整数的乘法: int multiply(int a, int b) { int ret = 0; for (int i=0; i<b; i++) { ret += b; } return
(x << 3) - x;
(x一个左移整数乘以2,前提是它不会溢出。只要在接近时进行适当的加或减即可。当归结到它时,可以像这样进行正整数的乘法:
int multiply(int a, int b) {
int ret = 0;
for (int i=0; i<b; i++) {
ret += b;
}
return ret;
}
或者类似的东西
换句话说,乘以7
- 左移2(乘以4)。左移3等于8,大于7
- 添加
b
3次
这与x*8-x=x*(8-1)=x*7
想想你是如何用铅笔和纸以十进制进行乘法的:
12
x 26
----
72
24
----
312
二进制乘法是什么样子的
0111
x 0101
-------
0111
0000
0111
-------
100011
注意到了吗?不像十进制乘法,你需要记住“时刻表”二进制乘法时,在将其中一个项写入列表加数之前,您总是将其乘以0或1。不需要时间表。如果第二个项的数字为1,则添加第一个项。如果为0,则不需要。还要注意加数是如何逐渐向左移动的
如果你不确定这一点,可以在纸上做一些二进制乘法。完成后,将结果转换回十进制,看看是否正确。做了几次之后,我想你会明白如何使用移位和加法实现二进制乘法。任何数字,无论奇数还是偶数,都可以表示为二的幂和。Fo例如
1 2 4 8
------------------
1 = 1
2 = 0 + 2
3 = 1 + 2
4 = 0 + 0 + 4
5 = 1 + 0 + 4
6 = 0 + 2 + 4
7 = 1 + 2 + 4
8 = 0 + 0 + 0 + 8
11 = 1 + 2 + 0 + 8
1x = x
2x = 0 + x<<1
3x = x + x<<1
4x = 0 + 0 + x<<2
5x = x + 0 + x<<2
11x = x + x<<1 + 0 + x<<3
因此,通过执行正确的移位和加法,您可以将x乘以任何数字
1x = x
2x = 0 + x<<1
3x = x + x<<1
4x = 0 + 0 + x<<2
5x = x + 0 + x<<2
11x = x + x<<1 + 0 + x<<3
1x=x
2x=0+x无符号整数乘法(无符号整数m1,无符号整数m2)
{
unsigned int numBits=sizeof(unsigned int)*8;//不是核心算法的一部分
无符号整数乘积=0;
无符号整数掩码=1;
对于(int i=0;iint乘法)(int被乘数,int因子)
{
如果(因子==0)返回0;
int乘积=被乘数;
对于(int ii=1;ii=0?产品:-产品;
}
你想要不带*
的乘法,明白了,伙计!@Wang,这是一个很好的推广。但是这里有一个稍微快一点的版本。但是它假设没有溢出,a是非负的
int mult(int a, int b){
int p=1;
int rv=0;
for(int i=0; a >= p && i < 31; i++){
if(a & p){
rv += b;
}
p = p << 1;
b = b << 1;
}
return rv;
}
intmult(inta,intb){
int p=1;
int-rv=0;
对于(inti=0;a>=p&&i<31;i++){
if(a&p){
rv+=b;
}
p=p很容易避免使用“*”运算符:
mov eax, 1234h
mov edx, 5678h
imul edx
看不到“*”。当然,如果您想了解它的精神,也可以使用可靠的旧shift和add算法:
mult proc
; Multiplies eax by ebx and places result in edx:ecx
xor ecx, ecx
xor edx, edx
mul1:
test ebx, 1
jz mul2
add ecx, eax
adc edx, 0
mul2:
shr ebx, 1
shl eax, 1
test ebx, ebx
jnz mul1
done:
ret
mult endp
当然,在现代处理器中,所有(?)都有乘法指令,但在处理器焕然一新的时候,像这样的代码才有了真正的用途。另一个跳出框框的想法是:
BigDecimal a = new BigDecimal(123);
BigDecimal b = new BigDecimal(2);
BigDecimal result = a.multiply(b);
System.out.println(result.intValue());
一天晚上,我发现自己非常无聊,于是胡编乱造:
#include <iostream>
typedef unsigned int uint32;
uint32 add(uint32 a, uint32 b) {
do {
uint32 s = a ^ b;
uint32 c = a & b;
a = s;
b = c << 1;
} while (a & b)
return (a | b)
}
uint32 mul(uint32 a, uint32 b) {
uint32 total = 0;
do {
uint32 s1 = a & (-(b & 1))
b >>= 1; a <<= 1;
total = add(s1, total)
} while (b)
return total;
}
int main(void) {
using namespace std;
uint32 a, b;
cout << "Enter two numbers to be multiplied: ";
cin >> a >> b;
cout << "Total: " << mul(a,b) << endl;
return 0;
}
#包括
typedef无符号整数uint32;
uint32添加(uint32 a、uint32 b){
做{
uint32 s=a^b;
uint32 c=a&b;
a=s;
b=c>=1;a>b;
问题是:
不使用*运算符将数字乘以7
这不使用*
:
number / (1 / 7)
编辑:
这在C语言中编译并运行良好:
int number,result;
number = 8;
result = number / (1. / 7);
printf("result is %d\n",result);
无符号整数乘法(无符号整数a,无符号整数b)
{
int-ret=0;
//对于b中的每一位
对于(int i=0;i,从数学上讲,乘法分布在加法上。本质上,这意味着:
x*(a+b+c…=(x*a)+(x*b)+(x*c)
任何实数(在您的例子中是7
)都可以表示为一系列加法(例如8+(-1)
,因为减法实际上只是一种错误的加法)。这允许您将任何单个乘法语句表示为一个等价的乘法语句系列,将得到相同的结果:
x * 7
= x * (8 + (-1))
= (x * 8) + (x * (-1))
= (x * 8) - (x * 1)
= (x * 8) - x
按位移位运算符本质上只是将一个数字乘以或除以2的幂。只要您的等式仅处理此类值,就可以使用位移位替换乘法运算符的所有出现次数
(x*8)-x=(x*23)-x=(x每个人都忽略了显而易见的事情。不涉及乘法:
10^(log10(A) + log10(B))
在C#中:
私有静态字符串多(int a,int b)
{
如果(a==0 | | b==0)
返回“0”;
bool为阴性=假;
if(a<0 | | b<0)
{
isnegative=true;
a=数学Abs(a);
b=数学Abs(b);
}
整数和=0;
如果(a>b)
{
对于(inti=1;i循环它。运行一个循环七次,然后用你要乘以七的数字进行迭代
伪代码:
total = 0
multiply = 34
loop while i < 7
total = total + multiply
endloop
total=0
乘=34
当i<7时循环
总计=总计+乘法
端环
如果您可以使用日志功能:
public static final long multiplyUsingShift(int a, int b) {
int absA = Math.abs(a);
int absB = Math.abs(b);
//Find the 2^b which is larger than "a" which turns out to be the
//ceiling of (Log base 2 of b) == numbers of digits to shift
double logBase2 = Math.log(absB) / Math.log(2);
long bits = (long)Math.ceil(logBase2);
//Get the value of 2^bits
long biggerInteger = (int)Math.pow(2, bits);
//Find the difference of the bigger integer and "b"
long difference = biggerInteger - absB;
//Shift "bits" places to the left
long result = absA<<bits;
//Subtract the "difference" "a" times
int diffLoop = Math.abs(a);
while (diffLoop>0) {
result -= difference;
diffLoop--;
}
return (a>0&&b>0 || a<0&&b<0)?result:-result;
}
public static final long multiplyUsingShift(int a, int b) {
int absA = Math.abs(a);
int absB = Math.abs(b);
//Get the number of bits for a 2^(b+1) larger number
int bits = 0;
int bitInteger = absB;
while (bitInteger>0) {
bitInteger /= 2;
bits++;
}
//Get the value of 2^bit
long biggerInteger = (int)Math.pow(2, bits);
//Find the difference of the bigger integer and "b"
long difference = biggerInteger - absB;
//Shift "bits" places to the left
long result = absA<<bits;
//Subtract the "difference" "a" times
int diffLoop = absA;
while (diffLoop>0) {
result -= difference;
diffLoop--;
}
return (a>0&&b>0 || a<0&&b<0)?result:-result;
}
public静态最终长倍频(inta,intb){
int absA=Math.abs(a);
int absB=数学abs(b);
//找到2^b,它比“a”大,而“a”是
//上限(b的对数基数2)=要移位的位数
double logBase2=Math.log(absB)/Math.log(2);
长位=(长)Math.ceil(logBase2);
//获取2^位的值
long biggerInteger=(int)Math.pow(2,位);
//求大整数和“b”的差
长差=大整数-absB;
//将“位”位置向左移动
long result=absA0&&b>0 | | a0 | | a0)result+=absB;//是奇数
absA>>=1;
absB 0 | | a0 | | apackage com.amit.string;
//这里我传递两个值,7和3,方法getResult()将
//返回21,不使用除递增运算符++以外的任何运算符。
//
公共类乘数{
公共静态void main(字符串[]ar
private static string Multi(int a, int b)
{
if (a == 0 || b == 0)
return "0";
bool isnegative = false;
if (a < 0 || b < 0)
{
isnegative = true;
a = Math.Abs(a);
b = Math.Abs(b);
}
int sum = 0;
if (a > b)
{
for (int i = 1; i <= b; i++)
{
sum += a;
}
}
else
{
for (int i = 1; i <= a; i++)
{
sum += b;
}
}
if (isnegative == true)
return "-" + sum.ToString();
else
return sum.ToString();
}
total = 0
multiply = 34
loop while i < 7
total = total + multiply
endloop
import java.math.BigInteger;
public class MultiplyTest {
public static void main(String[] args) {
BigInteger bigInt1 = new BigInteger("5");
BigInteger bigInt2 = new BigInteger("8");
System.out.println(bigInt1.multiply(bigInt2));
}
}
public static final long multiplyUsingShift(int a, int b) {
int absA = Math.abs(a);
int absB = Math.abs(b);
//Find the 2^b which is larger than "a" which turns out to be the
//ceiling of (Log base 2 of b) == numbers of digits to shift
double logBase2 = Math.log(absB) / Math.log(2);
long bits = (long)Math.ceil(logBase2);
//Get the value of 2^bits
long biggerInteger = (int)Math.pow(2, bits);
//Find the difference of the bigger integer and "b"
long difference = biggerInteger - absB;
//Shift "bits" places to the left
long result = absA<<bits;
//Subtract the "difference" "a" times
int diffLoop = Math.abs(a);
while (diffLoop>0) {
result -= difference;
diffLoop--;
}
return (a>0&&b>0 || a<0&&b<0)?result:-result;
}
public static final long multiplyUsingShift(int a, int b) {
int absA = Math.abs(a);
int absB = Math.abs(b);
//Get the number of bits for a 2^(b+1) larger number
int bits = 0;
int bitInteger = absB;
while (bitInteger>0) {
bitInteger /= 2;
bits++;
}
//Get the value of 2^bit
long biggerInteger = (int)Math.pow(2, bits);
//Find the difference of the bigger integer and "b"
long difference = biggerInteger - absB;
//Shift "bits" places to the left
long result = absA<<bits;
//Subtract the "difference" "a" times
int diffLoop = absA;
while (diffLoop>0) {
result -= difference;
diffLoop--;
}
return (a>0&&b>0 || a<0&&b<0)?result:-result;
}
public static final long multiplyUsingShift(int a, int b) {
int absA = Math.abs(a);
int absB = Math.abs(b);
long result = 0L;
while (absA>0) {
if ((absA&1)>0) result += absB; //Is odd
absA >>= 1;
absB <<= 1;
}
return (a>0&&b>0 || a<0&&b<0)?result:-result;
}
public static final long multiplyUsingLogs(int a, int b) {
int absA = Math.abs(a);
int absB = Math.abs(b);
long result = Math.round(Math.pow(10, (Math.log10(absA)+Math.log10(absB))));
return (a>0&&b>0 || a<0&&b<0)?result:-result;
}
package com.amit.string;
// Here I am passing two values, 7 and 3 and method getResult() will
// return 21 without use of any operator except the increment operator, ++.
//
public class MultiplyTwoNumber {
public static void main(String[] args) {
int a = 7;
int b = 3;
System.out.println(new MultiplyTwoNumber().getResult(a, b));
}
public int getResult(int i, int j) {
int result = 0;
// Check for loop logic it is key thing it will go 21 times
for (int k = 0; k < i; k++) {
for (int p = 0; p < j; p++) {
result++;
}
}
return result;
}
}
public static int multiply(int a, int b)
{
int temp = 0;
if (b == 0) return 0;
for (int ii = 0; ii < abs(b); ++ii) {
temp = temp + a;
}
return b >= 0 ? temp : -temp;
}
public static int abs(int val) {
return val>=0 ? val : -val;
}
/* Multiply two signed integers using the Booth algorithm */
int booth(int x, int y)
{
int prev_bit = 0;
int result = 0;
while (x != 0) {
int current_bit = x & 0x1;
if (prev_bit & ~current_bit) {
result += y;
} else if (~prev_bit & current_bit) {
result -= y;
}
prev_bit = current_bit;
x = static_cast<unsigned>(x) >> 1;
y <<= 1;
}
if (prev_bit)
result += y;
return result;
}
/* Multiply two 16-bit signed integers using the Booth algorithm */
/* Returns a 32-bit signed integer */
int32_t booth(int16_t x, int16_t y)
{
int16_t prev_bit = 0;
int16_t sign_bit = (x >> 16) & 0x1;
int32_t result = 0;
int32_t y1 = static_cast<int32_t>(y);
while (x != 0) {
int16_t current_bit = x & 0x1;
if (prev_bit & ~current_bit) {
result += y1;
} else if (~prev_bit & current_bit) {
result -= y1;
}
prev_bit = current_bit;
x = static_cast<uint16_t>(x) >> 1;
y1 <<= 1;
}
if (prev_bit & ~sign_bit)
result += y1;
return result;
}
public static void main(String[] args) {
System.out.print("Enter value of A -> ");
Scanner s=new Scanner(System.in);
double j=s.nextInt();
System.out.print("Enter value of B -> ");
Scanner p=new Scanner(System.in);
double k=p.nextInt();
double m=(1/k);
double l=(j/m);
System.out.print("Multiplication of A & B=> "+l);
}
public int multiply_optimal(int a, int b) {
if (a == 0 || b == 0)
return 0;
if (b == 1)
return a;
if ((b & 1) == 0)
return multiply_optimal(a + a, b >> 1);
else
return a + multiply_optimal(a + a, b >> 1);
}
1 = 2 ^ 0
2 = 2 ^ 1
3 = 2 ^ 1 + 2 ^ 0
...
1. while m is greater or equal to 2^pow:
1.1 get the biggest number pow, such as 2^pow is lower or equal to m
1.2 multiply n*2^pow and decrease m to m-2^pow
2. sum the results
long multiply(int n, int m) {
int pow = 0;
while (m >= (1 << ++pow)) ;
pow--;
if (m == 1 << pow) return (n << pow);
return (n << pow) + multiply(n, m - (1 << pow));
}
unsigned multiply(unsigned x, unsigned y) { return sizeof(char[x][y]); }
N x 7 = N + N + N + N + N + N + N
N x 7 = N + N + N + N + N + N + N + (N - N)
N x 7 = (N + N + N + N + N + N + N + N) - N
N x 7 = 8xN - N
N x 7 = (N << 3) - N
#include<stdio.h>
/* function to multiply two numbers x and y*/
int multiply(int x, int y)
{
/* multiplied with anything gives */
if(y == 0)
return 0;
/* Add x one by one */
if(y > 0 )
return (x + multiply(x, y-1));
/* the case where y is negative */
if(y < 0 )
return -multiply(x, -y);
}
int main()
{
printf("\n %d", multiply(5, -11));
getchar();
return 0;
}
function recursiveMultiply(num1, num2){
const bigger = num1 > num2 ? num1 : num2;
const smaller = num1 <= num2 ? num1 : num2;
const indexIncrement = 1;
const resultIncrement = bigger;
return recursiveMultiplyHelper(bigger, smaller, 0, indexIncrement, resultIncrement)
}
function recursiveMultiplyHelper(num1, num2, index, indexIncrement, resultIncrement){
let result = 0;
if (index === num2){
return result;
}
if ((index+indexIncrement+indexIncrement) >= num2){
indexIncrement = 1;
resultIncrement = num1;
} else{
indexIncrement += indexIncrement;
resultIncrement += resultIncrement;
}
result = recursiveMultiplyHelper(num1, num2, (index+indexIncrement), indexIncrement, resultIncrement);
result += resultIncrement;
console.log(num1, num2, index, result);
return result;
}
1101 x =>13
0101 =>5
---------------------
1101
0000
1101
0000
===================
1000001 . => 65
#include<stdio.h>
int multiply(int a, int b){
int res = 0,count =0;
while(b>0) {
if(b & 0x1)
res = res + (a << count);
b = b>>1;
count++;
}
return res;
}
int main() {
printf("Sum of x+y = %d", multiply(5,10));
return 0;
}