Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate decimal in range for 24 bit?

Tags:

python

php

math

I have 24 bit of binary, which is 111010001100001010011000 equals to 15254168

I only "guess" its 24 bit, because binary length is 24.

I would like to generate all 24 bit decimals programmaticly. (C, PHP or Python)

2**24 returns 16.777.216

So there are 16.777.216 other decimals (combinations). How can i generate them?

I can't understand the "range" of 24 bit. May someone help me on this?

Thanks.

like image 747
martyr Avatar asked Apr 26 '15 20:04

martyr


2 Answers

<!-- language: python -->

Is this what you want?

>>> n = 3
>>> result = [bin(k)[2:].rjust(n, '0') for k in xrange(2**n)]
>>> print result
['000', '001', '010', '011', '100', '101', '110', '111']
>>> n = 24 
like image 132
rafaelc Avatar answered Oct 16 '22 09:10

rafaelc


24bits just mean there are 24 bits(zeros or ones) that together create a binary number.

If you want all combinations, or all numbers that can be expressed with 24 bits, it is just range from 0 to 16777215. Why? here is the table in format (binary = decimal):

000000000000000000000000 = 0
000000000000000000000001 = 1
000000000000000000000010 = 2
000000000000000000000011 = 3
....
....
111111111111111111111110 = 16777214
111111111111111111111111 = 16777215

you dont really need to generate anything. You can check the binary to decimal here: http://www.binaryhexconverter.com/binary-to-decimal-converter

Another thing: Sometimes in binary, the leading zeros are omitted. So decimal three is not 000000000000000000000011 but rather just 11. If the length is 24 and first digit is 1, its still just range 8388608 - 16777215

like image 31
Martin Gottweis Avatar answered Oct 16 '22 09:10

Martin Gottweis