HP Forums
(48G/49/50) Gray Codes - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: General Software Library (/forum-13.html)
+--- Thread: (48G/49/50) Gray Codes (/thread-16634.html)



(48G/49/50) Gray Codes - John Keith - 04-10-2021 09:04 PM

This program returns a list of binary Gray codes from 0 to 2^n-1. For example, given the number 4 the program will return a list of Gray codes from 0 to 15.

If real numbers are desired instead of binary numbers, replace the binary numbers in line 1 with reals, and replace the SL in line 2 with 2 *.

Code:

\<< # 10b { # 0b # 1b } ROT 2 SWAP
  START DUP2 ADD REVLIST + SWAP SL SWAP
  NEXT SWAP DROP
\>>