Casio fx-CG50 and Swiss Micros DM32: HP 16C’s Bit Summation
The HP 16C’s #B Function
The #B function is the HP 16C’s number of bits function and sums the bits that are “turned on”. To find #B, if needed, convert the number to its binary form (base 2). #B are the number of ones. Here, I am assuming the binary integers are unsigned.
For example: Calculate #B(49). Assuming 49 is in decimal base.
49_10 = 110001_2
Then #B(49) = 3
Casio fx-CG 50 Program: BITS
I thought the fx-CG 50 had functions to convert integers and logical functions, but I did not have find them. If anyone knows whether the fx-CG 50 has base conversions, please let me know. I know that earlier Casio graphing calculators had base conversions.
The binary integer form is stored in B, as long as the decimal integer is less than 2048. (2^11).
Size: 168 bytes
“DEC→BIN”
“D”? → N
N → M
0 → C
0 → B
Int (log N ÷ log 2) → L
For L → J To 0 Step -1
If M ≥ 2^J
Then
C + 1 → C
M – 2^J → M
B × 10 + 1 → B
Else
B × 10 → B
IfEnd
Next
ClrText
If N < 2049
Then
“BIN=”
B ◢
IfEnd
“#B=”
C
The hashtag character (#) is called from the CHAR menu.
Swiss Micros DM32 Program: BITS
Two labels are used:
LBL B: 68 bytes
LBL T: 92 bytes
The main program is LBL B. This should fit on the classic HP 32S/32SII calculators.
B01 LBL B
B02 CF 0
B03 SF 10
B04 “DEC NUMB”
B05 PSE
B06 INPUT N
B07 STO M
B08 Clx
B09 STO C
B10 RCL N
B11 LOG
B12 2
B13 LOG
B14 ÷
B15 IP
B16 1
B17 +
B18 STO L
T01 LBL T
T02 RCL M
T03 2
T04 RCL L
T05 1
T06 -
T07 y^x
T08 -
T09 x≥0?
T10 SF 0
T11 FS? 0
T12 LASTx
T13 FS? 0
T14 STO- M
T15 FS? 0
T16 1
T17 FS? 0
T18 STO+ C
T19 CF 0
T20 DSE L
T21 GTO T
T22 RCL N
T23 BIN
T24 STOP
T25 “BITS=”
T26 PSE
T27 CF 10
T28 DEC
T29 VIEW C
T30 GTO B
Examples
Decimal: 35, Binary: 100011, #B(35) = 3
Decimal: 36, Binary: 100100, #B(36) = 2
Decimal: 37, Binary: 100101, #B(37) = 3
Decimal: 50, Binary: 110010, #B(50) = 3
Decimal: 51, Binary: 110011, #B(51) = 4
Decimal: 52, Binary: 110100, #B(52) = 3
Sources
Hewlett-Packard. HP-16C Computer Scientists Owner’s Handbook. Hewlett-Packard Company. 1982. pg. 52
Eddie
All original content copyright, © 2011-2024. Edward Shore. Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited. This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.