Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

11
  • 2
    you have got reversed bits in your function, so 20 will return 00101, not 10100 Commented Jan 31, 2012 at 13:56
  • 2
    you did not state whether you wanted big or little endian. The example didn't give it away either, since 111 is a palindrome ;). Anyway, adapting it is easy: just use nBits=ceiling(select(2,math.frexp(num))) and use a for-loop starting at nBits going to 1. Commented Jan 31, 2012 at 13:59
  • my fault, sorry, nevertheless the answer is right and useful, thank you! Commented Jan 31, 2012 at 14:00
  • I've added a most-significant version to your answer. I left off the call to math.ceil() because, as far as I can tell, frexp always returns an integer for the second value. Is there an edge case that I have missed? Commented Sep 26, 2014 at 3:45
  • No indeed, as per the manual on math.frexp, the second return value should be always integer. Thanks for the edit! Commented Sep 29, 2014 at 9:52