I have a small 8-bit processor which has a N-to-M decoder on some output lines - eg, for the 5 to 32 bit case, I write 00101 and bit 5 changes state. The only interface to the output is change-state, there is no read-back.
The device counts rapidly (but randomly) occuring events, and should provide this count as a 'single bit changes' code to another device. The output pins are read in parallel by another device, and may be read as rapidly or as sparingly as the other device decides, so the count is necessary.
I do NOT need to use the standard Binary Reflective Gray code - I can use any single-bit changing code.
However, I want to be able to track the next bit to change efficiently.
I do not have a "LowestBitSet" instruction, and finding lowest bit set across four 8 bit registers is cycle consuming - so I cannot use this "common" approach:
Keep binary counter A
Find B as A XOR (A+1)
Bit to change is LowestBitSet in B
I wish to calculate this in as little memory and registers as possible, and memory is definitely too restricted for any large lookup table. Cycle time is the more important factor.
Any suggestions on algorithms?
/*
Can not edit previous answer, as per commentary, so posting rewrite:
Too impatient?
For immediate gratification and minimal edification, cut to the chase and chase this link where only the final result has been posted:
C code for generating next bit to flip in a gray code
REFs:
C code for generating next bit to flip in a gray code
How do I find next bit to change in a Gray code in constant time?
Deriving nth Gray code from the (n-1)th Gray Code
Gray code increment function
Efficient way to iterate over Gray code change positions
Generating gray codes.
WARNING:
For purposes of coding expediency and demonstrable functional execution, some non-trivial programming techniques have been used. However, this is hopefully mitigated for the exposition of the concept rudiments by presenting the essence as trivially and minimally as possible with highlighting by / / / /. Careful reading, study and experiment are encouraged to avoid cargo cult coding, oversights, and perpetrating mistakes.
This answer is manifested in the Arduino IDE ESP8266 core coding environment.
The algorithm as posited by the OP is not exactly correct (as if this;).
The Johnson Code
-------------------------
Since the actual Gray coding is irrelevant, using a Johnson counter's Gray code is an exceptionally easy and poignant way to cognitively and computationally count both the bit to change and the next code.
Note that the Johnson counter Gray code density is linear and not logarithmic.
With 32 bits in 4 bytes, the sequence can count from 0 to 63 before resetting.
It is necessary to verify carefully the functional suitability of the code that follows, modifying it as appropriate.
HINT: Verification is a MUST, especially for the "binary reflected" Gray code (BRGC)!
/./ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /./
/./ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /./
/*
Outputs:
Some background material on the Binary Reflected Gray Code (BRGC)
-----------------------------------------------------------------------------------------------
CONVERSIONS:
---------------------
REF: Code Golf: Gray Code
COUNTING:
-----------------
The following (as per ref. above) arbitrarily gets both the preceding and following BRGC's for a code.
NB! The order of
n1
andn2
is parity determined and non-corresponding otherwise. The ordering might ben1, gray, n2
OR it could ben2, gray, n1
, so,eo
(parity) discriminates.ie.
hence
/./ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /./
/./ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /.
REF: The neighbors in Gray code
Oh yeah, ... count set bits in
A ^ A+1
which will have a bit pattern like 000...0111..1 Prove.How to get bit position for a power of 2 - the
n
parameters must have a single bit set.Method 1
*/
/*
Method 2
*/
/*
Method 3
*/
/*
Method 4
*/
/*
Method 5
Details are at the end.
Can a judicious choice of constants reduce this to only 2 operations?
*/
/*
Testing Environment
*/
/======================================================================
Caveat:
-----------
The OP's algorithm is not effective.
as seen when coded as:
*/
/*
Sample output:
Curious?
-----------
The following code is a very very crude method that can expedite a hunt for suitable bit packed counting candidates to compute
log 2^n
(in base 2 ie.n
).*/
/*
*/