HP 50g Binary Number Formatting
|
04-20-2022, 05:47 PM
Post: #1
|
|||
|
|||
HP 50g Binary Number Formatting
Is it possible to have binary numbers displayed in groups of 4 digits? e.g. 1110 1101
|
|||
04-23-2022, 01:38 PM
(This post was last modified: 04-23-2022 10:14 PM by Gil.)
Post: #2
|
|||
|
|||
RE: HP 50g Binary Number Formatting
1) Place your binary digits as a string in the stack.
"1110 1101" 2) Execute —>BIN4 (file here included as "—>BIN4.Doc" Note: - The ending .Doc is not a Word document. - You can simply download that file into your calculator - and save it in your calculator as —>BIN4). Code for —>BIN4: « DUP DUP SIZE "" str.ORIGIN size.ORIGIN str.NEW « size.ORIGIN 4 MOD 4 SWAP - DUP m m.ORIGIN « size.ORIGIN m + 4 / loop « WHILE m 0 REPEAT " " str.ORIGIN + 'str.ORIGIN' STO -1 'm' STO+ END 1 loop FOR i str.NEW str.ORIGIN i 1 - 4 * 1 + DUP 3 + SUB + " " + 'str.NEW' STO NEXT str.NEW m.ORIGIN 4 MOD 1 + str.NEW SIZE 1 - SUB » » » » Or \\<< DUP DUP SIZE \"\" \\-> str.ORIGIN size.ORIGIN str.NEW \\<< size.ORIGIN 4 MOD 4 SWAP - DUP \\-> m m.ORIGIN \\<< size.ORIGIN m + 4 / \\-> loop \\<< WHILE m 0 \\=/ REPEAT \" \" str.ORIGIN + 'str.ORIGIN' STO -1 'm' STO+ END 1 loop FOR i str.NEW str.ORIGIN i 1 - 4 * 1 + DUP 3 + SUB + \" \" + 'str.NEW' STO NEXT str.NEW m.ORIGIN 4 MOD 1 + str.NEW SIZE 1 - SUB \\>> \\>> \\>> \\>> It's a rough solution that should work fine, though it could be bettered in terms of code. Hope it might help. Regards, Gil Campart |
|||
04-23-2022, 06:01 PM
Post: #3
|
|||
|
|||
RE: HP 50g Binary Number Formatting
Better add the bold instructions
4 MOD in the modified code below: \<< DUP DUP SIZE "" \-> str.ORIGIN size.ORIGIN str.NEW \<< size.ORIGIN 4 MOD 4 SWAP - 4 MOD DUP \-> m m.ORIGIN \<< size.ORIGIN m + 4 / \-> loop \<< WHILE m 0 \=/ REPEAT " " str.ORIGIN + 'str.ORIGIN' STO -1 'm' STO+ END 1 loop FOR i str.NEW str.ORIGIN i 1 - 4 * 1 + DUP 3 + SUB + " " + 'str.NEW' STO NEXT str.NEW m.ORIGIN 4 MOD 1 + str.NEW SIZE 1 - SUB \>> \>> \>> \>> |
|||
04-23-2022, 06:50 PM
Post: #4
|
|||
|
|||
RE: HP 50g Binary Number Formatting
Easier to understand the following code, where we take every value and check if it is the 4th, the 8th,the 12th..., and when it is the case add a blank.
Code for —>BIN4c: \<< \->STR DUP DUP SIZE \-> str.ORIGIN size.ORIGIN \<< size.ORIGIN 4 MOD 4 SWAP - 4 MOD \-> m \<< m m WHILE m 0 \=/ REPEAT " " str.ORIGIN + 'str.ORIGIN' STO -1 'm' STO+ END size.ORIGIN + 1 SWAP "" UNROT FOR i str.ORIGIN i i SUB + i 4 MOD 0 == IF THEN " " + END NEXT DUP SIZE 1 - ROT 1 + SWAP SUB \>> \>> \>> |
|||
04-23-2022, 11:42 PM
Post: #5
|
|||
|
|||
RE: HP 50g Binary Number Formatting
A somewhat different approach:
Code:
Note that the string in line 4 is one space, and the string in line 7 is an empty string. The reason for the empty string in line 7 is that \GSLIST (Sigma-list) can't handle lists with less than two elements. |
|||
04-24-2022, 12:03 AM
(This post was last modified: 04-24-2022 12:06 AM by Gil.)
Post: #6
|
|||
|
|||
RE: HP 50g Binary Number Formatting
It seems that, with the program suggested by John Keith, the 4 digits-block starts from the left.
So that we get with his program: 1234 5678 1234 5678 9 1234 5678 91 1234 5678 912 But I understood that we should get: 123 4567 8912 12 3456 7891 1 2345 6789 1234 5678 Besides, John Keith's program seems to cut off the first and last digits. But really a great idea to play with the stack, not using — at all — intermediary variables. Regards, Gil |
|||
04-24-2022, 10:40 AM
Post: #7
|
|||
|
|||
RE: HP 50g Binary Number Formatting
(04-24-2022 12:03 AM)Gil Wrote: It seems that, with the program suggested by John Keith, the 4 digits-block starts from the left.Correct. It'll have to work in reverse - replace Code: DUP 1. 4. SUB Code: DUPDUP SIZE DUP 3. - SWAP SUB Code: 5. OVER SIZE SUB Code: 1. OVER SIZE 4. - SUB Code: \->LIST "" + \GSLIST Code: 2. SWAP START + NEXT All of the above suggestions are untested, but should work. Otherwise, complain or get to work yourself. (04-24-2022 12:03 AM)Gil Wrote: Besides, John Keith's program seems to cut off the first and last digits.Incorrect, it merely cuts off the "# " prefix and "b" suffix. (04-24-2022 12:03 AM)Gil Wrote: But really a great idea to play with the stack, not using — at all — intermediary variables.As do the majority of good RPL programs, because in most cases variables are both bulkier and slower. If readability is a concern, we can have comments instead of variable names - they can be preserved by encasing the whole program in a string. Stash a copy of that somewhere (the SD card should have plenty of space to spare), and use STR\-> to compile it. The ability to view and edit a compiled program was useful when our beloved calculators were short on memory, but now it has become a trap - embrace the standard software development process of keeping source and binary separate. Anyway, back to topic... I like a programming mini-challenge, so I've taken a swing at an implementation of my own. It's in SysRPL because of course it is, what else would you expect from a SysRPL enthusiast - and the trick at its center is definitely a bit unorthodox. Some may find it inspiring, others horrifying: instead of the string chopping of the previous programs, I'm slicing the number into 4-bit pieces with arithmetic and logic operations (so far, so normal), conducting further arithmetic and logic operations on each piece to construct the ASCII codes of the corresponding 5-character string in the binary number (uhhh...) and then charging ahead with string manipulation on it (what!?!). That works because a user binary integer (or hex string, in SysRPL terms) has the same memory layout as a character string, just a different prolog. I used to have # 2A2C CHANGETYPE in there, but then I remembered it wasn't necessary because most SysRPL commands don't even check for correct types, they just assume the programmer did it already. The rest is just putting the string pieces together and converting the most significant digit group using one of the normal convert-to-string commands because that one is allowed to omit leading zeroes. Despite this deliberate type confusion inside, the program has proper type checking for user input, and it even switches to BIN mode and sufficient word size for the duration of the program, while restoring the original flags afterwards. It also adds the "# " prefix and "b" suffix - which means converting the string back into a usable number only requires stripping out the spaces (e.g. in UserRPL: " " "" SREPL) followed by OBJ\->. Code: :: However, no programming marvel can hide the fact that binary integers on the 50g do not support digit grouping natively. (Nor do any other number formats, for that matter. The only one that comes somewhat close is the engineering mode for real numbers, which coerces the decimal exponent into a multiple of 3.) We can write programs to convert the number into an appropriately formatted string, we could even write more programs to convert it back into a number you can calculate with, but as soon as you use these with any kind of regularity, you'll find that borderline unusable. There is a different option, though. What is equivalent to four binary digits? A hexadecimal digit. Yes, I do propose simply using HEX mode instead. Performing the conversion between a hex digit and its binary equivalent in your head takes some practice, but it won't take long before that gets handed off to the subconscious. In addition to the restored calculator usability, there's another bonus: no more scrolling through obnoxiously long numbers on the small screen, because even a 64-bit number (the longest allowed) will just fit. |
|||
04-24-2022, 11:34 AM
(This post was last modified: 04-24-2022 11:36 AM by Gil.)
Post: #8
|
|||
|
|||
RE: HP 50g Binary Number Formatting
Great indeed all the refined solutions proposed.
Beyond my basic level, indeed. About mine, for which it seems easier to follow the steps: I considered a string of digits only such as "1111001". If you have instead a character at the beginning and another one at the end, such as "A1111001B", you will have to cut them off (the A and B in the example sbove) before launching the program. Add rather then the following instructions DUP SIZE 1 - 2 SWAP SUB at the very beginning of my program —>BIN4c |
|||
04-24-2022, 11:59 AM
Post: #9
|
|||
|
|||
RE: HP 50g Binary Number Formatting
(04-24-2022 10:40 AM)3298 Wrote: ... Wow, every time I get ready to post a response on this thread, someone posts something that makes it look as if I were plagiarizing. I took a different view to this, mostly because I prefer to see full groups of 4 (ie. with leading zeros) instead of a shortened version for the initial digit group. In other words, I would prefer to convert #1Fh to "0001 1111" than "1 1111". My standard UserRPL version of this would seem redundant given the previous posts. So instead I'll submit a version that uses some of the ListExt library commands to shorten the code: Code: \<< Descriptions of the ListExt commands used: S→SL: Converts a string to a list of individual characters LDROP: Drops the specified quantity of list elements from a given list (positive drops from the left, negative from the right) LSDIV: Subdivides a given list into a list of sublists with the given size LMAP: Executes the given object for each list element, and returns the results as a list LXIL: (eXplode Inner Lists) replaces top-level sublists with the individual elements they contain SL→S: builds a string from the given list of characters |
|||
04-24-2022, 06:06 PM
(This post was last modified: 04-24-2022 08:22 PM by dlidstrom.)
Post: #10
|
|||
|
|||
RE: HP 50g Binary Number Formatting
Here’s an implementation that accepts a real and converts it into a string.
→BIN4: r → s Code:
This pattern matching approach trades some bytes for readability, which I tend to favour these days (when programming in other languages). I didn’t find a string join function that could avoid the last if statement. Unfortunately STREAM errors on a list with a single element. 2xHP48GX, HP 50g, two Retrotronik ram cards, DM42 /Daniel Lidström |
|||
04-25-2022, 03:22 PM
Post: #11
|
|||
|
|||
RE: HP 50g Binary Number Formatting
(04-24-2022 10:40 AM)3298 Wrote: As do the majority of good RPL programs, because in most cases variables are both bulkier and slower. Such antiquated concept... newRPL variables are often faster than the stack. Anyway, here's my take in newRPL dialect: Code:
|
|||
04-25-2022, 04:04 PM
Post: #12
|
|||
|
|||
RE: HP 50g Binary Number Formatting
Sorry for my ignorance.
Where does the command BLSR in the last program, come from? Should I load a special program to access it? I use emu48 application on my phone exclusively. Thanks for your answer. Regards |
|||
04-25-2022, 04:39 PM
(This post was last modified: 04-25-2022 04:44 PM by Claudio L..)
Post: #13
|
|||
|
|||
RE: HP 50g Binary Number Formatting
(04-25-2022 04:04 PM)Gil Wrote: Sorry for my ignorance. It's a newRPL only command, similar to the SR command on the 50g ROM except this one takes the number of bits you want to shift, rather than just shifting by one. The code I provided was only meant to be used with newRPL, but you can replace 4 BLSR with SR SR SR SR (4 times 1-bit shifts) and it should work the same way on the stock ROM. Or you can download newRPL Desktop and try it on your PC. You can simply copy the text from my post to your clipboard and select Stack -> "Paste to Level 1" to get the text on the simulator, then do STR→ to get the program compiled and ready to run. **EDIT**: I just saw you use your phone exclusively. When you click the Download button it will download the PC version, but you can go instead to the Files tab, then you can go into the folder TARGET-ANDROID and download an .apk to install on your Android phone (assuming you use Android, if you have an iPhone you are out of luck...) |
|||
04-25-2022, 06:34 PM
Post: #14
|
|||
|
|||
RE: HP 50g Binary Number Formatting
(04-24-2022 12:03 AM)Gil Wrote: It seems that, with the program suggested by John Keith, the 4 digits-block starts from the left. Sorry about that, I posted on my way out and didn't check the program well enough. As 3298 pointed out, when you enter a binary number using # or convert an integer to binary using B->R, the number format is # 1010b (or d, h or o depending on mode). The program removes the leading "# " and the trailing letter. If your binary numbers come from another source, that part of my program (and others) can be removed. |
|||
04-25-2022, 10:34 PM
Post: #15
|
|||
|
|||
RE: HP 50g Binary Number Formatting
I understood afterwards, thinking at the very beginning that the input was composed of the only digits 1 and 0.
Thanks again. Regards, Gil |
|||
04-27-2022, 08:46 PM
Post: #16
|
|||
|
|||
RE: HP 50g Binary Number Formatting
Thank you all for these solutions. I'm learning more about this great calculator.
|
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 5 Guest(s)