On Thu, Sep 14, 2023 at 10:10 AM Riccardo (Jack) Lucchetti
<p002264(a)staff.univpm.it> wrote:
On 14/09/2023 14:16, Cottrell, Allin wrote:
[...]
> I notice that Matlab's dec2bin employs a string composed of
'0's and
> '1's to represent a binary number. Maybe we want to do that too? In
> its native form an unsigned integer occupies 4 bytes = 32 bits. If
> converted a la Matlab that goes up to 33 bytes = 264 bits (allowing
> for the terminating NUL byte). As currently documented in git, our
> representation occupies 32 * 8 = 256 bytes or 2048 bits, since a
> double is used to represent each bit of the integer. This seems kinda
> profligate in terms of the storage used.
I agree. However, our current implementation makes it possible to
convert several items at once, as in, for example "dec2bin(seq(1,6)')"
or "bin2dec(I(4))".
Granted, but a string-oriented variant could return an array of
strings to allow this sort of thing.
A less contrived example could be given by considering a
non-recursive
algorithm for computing all possible binary vectors of size n that sum
to k (useful, for example in conditional or fixed-effects logit models):
<hansl>
n = 5
k = 3
tmp = dec2bin(seq(2^k-1, 2^n-1)')[,1:n]
eval selifr(tmp, sumr(tmp) .= k)
</hansl>
That would surely require more work if using strings, but I think it
would be fairly straightforward to write an efficient routine to
return the number of 1s in the binary expansion of an integer (and a
vectorized variant of same).
Allin