when using integer class such as int8 in matlab, and some calculation result is bigger than the upper range (for int8 , the upper range is 127), matlab would automatically set the value to the maximum.
eg.
a=int8(126);
b=int8(2);
c=a+b; % matlab would gives c=127, since 128 exceed the upper range of int8
but in some cases, i want to get a negetive result, like many programming languages do
i want c to be -128 because 01111110+00000010=10000000 and it's -128 for int8
this could be done by a "bitadd" like
function int1 = bitadd(int1,int2)
while (int2 ~=0)
carry=bitand(int1,int2);
int1=bitxor(int1,int2);
int2=bitshift(carry,1);
end
end
however this is very inefficient, since a while loop in the function
Setting the result to maximum should be additional correction, so is there a faster method to do bitwise add without such correction?

 채택된 답변

Jan
Jan 2021년 9월 14일

1 개 추천

a = int8(126);
b = int8(2);
tic
for k = 1:1e4
c = bitadd1(a, b);
end
toc
Elapsed time is 0.141335 seconds.
tic
for k = 1:1e4
c = bitadd2(a, b);
end
toc
Elapsed time is 0.068172 seconds.
function int1 = bitadd1(int1,int2)
while (int2 ~=0)
carry = bitand(int1,int2);
int1 = bitxor(int1,int2);
int2 = bitshift(carry,1);
end
end
function c = bitadd2(a, b)
m = typecast(int16(a) + int16(b), 'int8');
c = m(1);
end

댓글 수: 3

If it's important to support nonscalars, your version can do it with just a tiny modification:
function c = bitadd2(a, b)
m = typecast(int16(a) + int16(b), 'int8');
c = m(1:2:end);
end
Depending on how the rest of the code calls this adding function, using the vectorized approach may be the biggest part of time saving:
% emulate doing the same number of operations in vectorized form
aa = repmat(a,[1 1E4]);
bb = repmat(b,[1 1E4]);
tic
c = bitadd2(aa,bb);
toc
On my dumpster PC in R2019b, this takes about 210us. Compare that to 67ms and 25ms for the loop tests with bitadd1() and bitadd2() respectively.
thanks for your bitadd2 and introducing typecast to me
in fact i want some code as fast as
tic;
for i=1:1e4
c=a+b;
end
toc;
since i think setting the result to maximum is additional correction and would take extra time.
But now i think when i have such performance requirement, it's better to use c++/fortran instead of matlab.
Jan
Jan 2021년 9월 23일
It will be much faster to use int16 in general and crop the higher bits during the addition. In my tests this is 10 times faster, but I did not get the correct results when the overflow appears.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

제품

태그

질문:

2021년 9월 14일

댓글:

Jan
2021년 9월 23일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by