[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: |
Jones, Keith @ IEC |
Subject: |
RE: Convert a number to its binary representation (not string) as 1D Vector |
Date: |
Tue, 10 Oct 2006 08:32:42 -0700 |
Muthiah
You inspired a variation
twos_pow = bitshift(1,[L:-1:0]);
bitvec=(bitand(N,twos_pow) == twos_pow);
The surprising thing (to me) is that your version is about 25% faster. I
tested by putting each version in a loop with 1e5 iterations. I assumed
shifting would be faster than exponentiation.
Keith R Jones
L-3 Communications
(714) 758-3103 (office)
(949) 275-2070 (cell)
-----Original Message-----
From: Muthiah Annamalai [mailto:address@hidden
Sent: Tuesday, October 10, 2006 7:52 AM
To: address@hidden
Subject: Re:Convert a number to its binary representation (not string) as 1D
Vector
Hello there,
A nice way of obtaining bitvector representation from a number
could be like this:
#Let N be some number.
#Let L be the bitlength for floor(log2(N))
twos_pow=2.^(L:-1:0)
bitvec=(bitand(N,twos_pow) == twos_pow);
This seemingly avoids user-level loops.
Thanks
Muthu
PS: Sorry for re-opening the old thread, just thought this was cool.
_______________________________________________
Help-octave mailing list
address@hidden
https://www.cae.wisc.edu/mailman/listinfo/help-octave