06-30-2020, 10:12 PM
Hi all,
At the moment I am making some programs for digital technology. Now I'm working on a program to count the ones in a binary integer. So, if you have the number #264h (#1001100100b) as input, the number 4 appears as the result. It works the same as the #B function on the HP-16C or the nBITS function on the WP-34s. Now an error has crept into this program that I don't understand. If I input the number # 192837645h (# 110010010100000110111011001000101b) as input, I get the number 14 as a result, which should have been 15. The strange thing is that the outcome is correct for other inputs. The error occurs during debugging, at the BITAND function with mask # 0F0F0F0F0F0F0F0Fh, see the code below (I use double lowercase letters for my variable names to distinguish them from built-in variables). Attached is the HP-50g program from which I derived it, which works correctly. Can anyone help me with this problem? Sincerely, Karel.
At the moment I am making some programs for digital technology. Now I'm working on a program to count the ones in a binary integer. So, if you have the number #264h (#1001100100b) as input, the number 4 appears as the result. It works the same as the #B function on the HP-16C or the nBITS function on the WP-34s. Now an error has crept into this program that I don't understand. If I input the number # 192837645h (# 110010010100000110111011001000101b) as input, I get the number 14 as a result, which should have been 15. The strange thing is that the outcome is correct for other inputs. The error occurs during debugging, at the BITAND function with mask # 0F0F0F0F0F0F0F0Fh, see the code below (I use double lowercase letters for my variable names to distinguish them from built-in variables). Attached is the HP-50g program from which I derived it, which works correctly. Can anyone help me with this problem? Sincerely, Karel.
Code:
HP-50g program:
::
CK1&Dispatch
BINT11
::
HXS 00010 7777777777777777
OVER
bitSR
OVER
bitAND
2DUP
bitSR
bitAND
ROTOVER
bitSR
bitAND
bit+
bit+
bit-
DUP
bitSR
bitSR
bitSR
bitSR
bit+
HXS 00010 F0F0F0F0F0F0F0F0
bitAND
HXS 00010 FF00000000000000
2DUP
bit/
bit*
bit-
HXS>#
UNCOERCE
;
;
@
HP Prime program:
#pragma mode(separator(.,;) integer(h32))
EXPORT nBITS(xx)
BEGIN
LOCAL mask,aa,bb;
LOCAL cc,dd,ee,ff;
LOCAL gg,hh,ii,jj;
LOCAL kk,ll,mm,nn;
LOCAL oo,pp,qq,rr;
LOCAL ss,tt;
mask:=#7777777777777777h;
aa:=BITSR(xx);
bb:=BITAND(aa,mask);
cc:=BITSR(bb);
dd:=BITAND(cc,mask);
ee:=BITSR(dd);
ff:=BITAND(ee,mask);
gg:=dd+ff;
hh:=bb+gg;
ii:=xx-hh:
jj:=ii;
kk:=BITSR(ii);
ll:=BITSR(kk);
mm:=BITSR(ll);
nn:=BITSR(mm);
oo:=jj+nn;
mask:=#0F0F0F0F0F0F0F0Fh;
pp:=BITAND(oo,mask);
qq:=pp/#FF;
rr:=qq*#FF;
ss:=pp-rr;
tt:=B->R(ss);
RETURN tt;
END;