help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Convert a number to its binary representation (not string) as 1D Vec


From: John W. Eaton
Subject: RE: Convert a number to its binary representation (not string) as 1D Vector
Date: Tue, 10 Oct 2006 11:53:21 -0400

On 10-Oct-2006, Jones, Keith  @ IEC wrote:

| Muthiah
| 
| You inspired a variation
| 
| twos_pow = bitshift(1,[L:-1:0]);
| bitvec=(bitand(N,twos_pow) == twos_pow);

Or, what about

  bitand(N,twos_pow) != 0

instead of comparing == twos_pow?  Then you can further simplify (or
at least compactify, if you'll allow me to invent that word):

  bitand(N,bitshift(1,floor(log2(N)):-1:0)) != 0

Hmm.  If I saw this in some code, I'd hope to have it wrapped in a
function with a meaningful name, or to have a comment above it
explaining the intent, which no longer seems so obvious.  Also, it is
a logical vector, so you may need to wrap it in double().

Finally, what is the advantage of

  bitshift(1,[L:-1:0])

over

  2.^(L:-1:0)

?

jwe


reply via email to

[Prev in Thread] Current Thread [Next in Thread]