Post Reply 
Problem in counting program
06-30-2020, 10:12 PM (This post was last modified: 06-30-2020 10:13 PM by cahlucas.)
Post: #1
Problem in counting program
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.

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;

I use HP-16C, WP-34S emulator, HP-35s, HP-48GX, HP-50g, and HP Prime G2.
Find all posts by this user
Quote this message in a reply
07-01-2020, 04:52 AM (This post was last modified: 07-01-2020 04:56 AM by Joe Horn.)
Post: #2
RE: Problem in counting program
Try that input again after setting the wordsize to 64 bits (in Home Settings). Prime's default of 32 bits will cause the problem you're seeing. The 50g's default wordsize is 64 bits.

<0|ɸ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
07-01-2020, 05:07 AM
Post: #3
RE: Problem in counting program
The following are general concepts that I am not sure if this is your case, nor with debugging.
- To send a 64-bit integer you must have the variable "Bits" in 64, also from configuration in order to define the default input length, or also send an integer with the suffix #FF:64h.
- Pragma must also obey this → pragma( integer(h64) ), you currently have h32, which is truncating your masks. If pragma is not defined, the Home setting will be used for the length of the integer.

#192837645h may be converted to #92837645h (result 14) due to the mask altered by the h32 pragma and otherwise by the Home setting.

Viga C | TD | FB
Visit this user's website Find all posts by this user
Quote this message in a reply
07-01-2020, 06:55 AM
Post: #4
RE: Problem in counting program
(07-01-2020 04:52 AM)Joe Horn Wrote:  Try that input again after setting the wordsize to 64 bits (in Home Settings). Prime's default of 32 bits will cause the problem you're seeing. The 50g's default wordsize is 64 bits.

Dear Mr. Horn,
The default wordsize was allready altered to 64 by me when initialling the calculator, so this is not causing the error. Thanks anyway. Sincerely, Karel.

I use HP-16C, WP-34S emulator, HP-35s, HP-48GX, HP-50g, and HP Prime G2.
Find all posts by this user
Quote this message in a reply
07-01-2020, 07:01 AM
Post: #5
RE: Problem in counting program
(07-01-2020 05:07 AM)Carlos295pz Wrote:  The following are general concepts that I am not sure if this is your case, nor with debugging.
- To send a 64-bit integer you must have the variable "Bits" in 64, also from configuration in order to define the default input length, or also send an integer with the suffix #FF:64h.
- Pragma must also obey this → pragma( integer(h64) ), you currently have h32, which is truncating your masks. If pragma is not defined, the Home setting will be used for the length of the integer.

#192837645h may be converted to #92837645h (result 14) due to the mask altered by the h32 pragma and otherwise by the Home setting.

Dear Mr. Carlos,
The pragma setting of h32 was indeed the cause of the error. After setting this to h64, the answer (15) turned out to be good. Thank you very much! Sincerely, Karel.

I use HP-16C, WP-34S emulator, HP-35s, HP-48GX, HP-50g, and HP Prime G2.
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)