How to generate decimal in range for 24 bit? - php

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.

<!-- 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

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

Related

How do i convert an 8 bit binary data to hex

print_r(bin2hex("11111111"));
echo '</br>';
print_r(bindec("11111111"));
Result
131313131313131
255
I want a hexadecimal 16 byte value to do aes encryption.How is the conversion from binary to hex happening in php.I am getting incorrect value using the function.Also when i convert an array of hexadecimal values to string the byte length changes
You get a correct result, it's just not what you want. bin2hex() returns an ASCII string of the hexadecimal representation. A quote from the manual:
Returns an ASCII string containing the hexadecimal representation of str.
So If you want the hexadecimal number you can use this:
print_r(dechex(bindec("11111111")));
The converter to get hexidecimal is dechex(), but it needs a decimal number. To do that we convert you binary string to a decimal number first using bindec() and then pass it into dechex(), e.g:
print_r(dechex(bindec("11111111")));
<?php
$str = "Hello world!";
echo bin2hex($str) . "<br>";
echo pack("H*",bin2hex($str)) . "<br>";
?>
PHP.NET Manual :
http://php.net/manual/en/function.bin2hex.php
Test Your Result : http://www.cs.princeton.edu/courses/archive/fall07/cos109/bc.html
Detailed Explanation:
http://www.computerhope.com/binhex.htm
It's simply 9 * 16 + F where F is 15 (the letters A thru F stand for 10 thru 15). In other words, 0x9F is 159.
It's no different really to the number 314,159 being:
3 * 100,000 (10^5, "to the power of", not "xor")
+ 1 * 10,000 (10^4)
+ 4 * 1,000 (10^3)
+ 1 * 100 (10^2)
+ 5 * 10 (10^1)
+ 9 * 1 (10^0)
for decimal (base 10).
The signedness of such a number is sort of "one level up" from there. The unsigned value of 159 (in 8 bits) is indeed a negative number but only if you interpret it as one.

Convert Gigabytes to Bytes

I am in the position where I am trying to convert gigabytes to bytes from a submit form. I have searched around and I am unable to find anything suitable.
Currently when converting bytes to gigabytes I use this method, which works perfectly.
public function byteFormat($bytes, $unit = "", $decimals = 2)
{
$units = array('B' => 0, 'KB' => 1, 'MB' => 2, 'GB' => 3, 'TB' => 4,
'PB' => 5, 'EB' => 6, 'ZB' => 7, 'YB' => 8);
$value = 0;
if ($bytes > 0) {
// Generate automatic prefix by bytes
// If wrong prefix given
if (!array_key_exists($unit, $units)) {
$pow = floor(log($bytes)/log(1024));
$unit = array_search($pow, $units);
}
// Calculate byte value by prefix
$value = ($bytes/pow(1024,floor($units[$unit])));
}
// If decimals is not numeric or decimals is less than 0
// then set default value
if (!is_numeric($decimals) || $decimals < 0) {
$decimals = 2;
}
// Format output
return sprintf('%.' . $decimals . 'f '.$unit, $value);
}
There seems to be plenty of examples of bytes to other formats but not the other way around.
I have seen that I can convert the number 1.5 like so
round(($number[0] * 1073741824));
The result is 12992276070, however, when using the byteformat method shown above, I get the following 1610612736, this seems quite a difference between the two methods.
Can anyone suggest a more stable method for converting gigabytes to bytes.
Well there are two different unit symbol, decimal and binary.
As you can see here, decimal multiplication is by 1000 and binary by 1024.
so if you are using "B"(byte), just do something like:
$bytenumber=$giga*pow(1024,3);
if using "b"(bit) :
$bitnumber=$giga*pow(1000,3);
P.S:$giga is your giga number.
You can only get as accurate of a conversion as there are numbers after the decimal place. If you start with 1.29634 gigs you'll get a closer representation to it's actual byte value versus calling it 1.3 Gigs. Is that what you're after?
numberOfBytes = round (numberOfGb * 1073741824)
is the exact answer to your question. It seems, you have miscalculated. Try to check it on a calculator.
The other problem is that if you have the source number of 2 digits, it is incorrect to give an answer in more or less than 2 digits. The correct counting will be:
source: 1.5GB
counting: 1.5GB*1073741824 B/GB= 1610612736 B
rounding to the last significant digit: 1610612736 B ~= 1.6e9 B
answer: 1.6e9 B
But, of course, many clients do not really want the correct answer, they want THEIR answer. It is up to you to choose.

Is there a clever way to do this with pure math

I've got this spot of code that seems it could be done cleaner with pure math (perhaps a logarigthms?). Can you help me out?
The code finds the first power of 2 greater than a given input. For example, if you give it 500, it returns 9, because 2^9 = 512 > 500. 2^8 = 256, would be too small because it's less than 500.
function getFactor($iMaxElementsPerDir)
{
$aFactors = range(128, 1);
foreach($aFactors as $i => $iFactor)
if($iMaxElementsPerDir > pow(2, $iFactor) - 1)
break;
if($i == 0)
return false;
return $aFactors[$i - 1];
}
The following holds true
getFactor(500) = 9
getFactor(1000) = 10
getFactor(2500) = 12
getFactor(5000) = 13
You can get the same effect by shifting the bits in the input to the right and checking against 0. Something like this.
i = 1
while((input >> i) != 0)
i++
return i
The same as jack but shorter. Log with base 2 is the reverse function of 2^x.
echo ceil(log(500, 2));
If you're looking for a "math only" solution (that is a single expression or formula), you can use log() and then take the ceiling value of its result:
$factors = ceil(log(500) / log(2)); // 9
$factors = ceil(log(5000) / log(2)); // 13
I seem to have not noticed that this function accepts a second argument (since PHP 4.3) with which you can specify the base; though internally the same operation is performed, it does indeed make the code shorter:
$factors = ceil(log(500, 2)); // 9
To factor in some inaccuracies, you may need some tweaking:
$factors = floor(log($nr - 1, 2)) + 1;
There are a few ways to do this.
Zero all but the most significant bit of the number, maybe like this:
while (x & x-1) x &= x-1;
and look the answer up in a table. Use a table of length 67 and mod your power of two by 67.
Binary search for the high bit.
If you're working with a floating-point number, inspect the exponent field. This field contains 1023 plus your answer, except in the case where the number is a perfect power of two. You can detect the perfect power case by checking whether the significand field is exactly zero.
If you aren't working with a floating-point number, convert it to floating-point and look at the exponent like in 3. Check for a power of two by testing (x & x-1) == 0 instead of looking at the significand; this is true exactly when x is a power of two.
Note that log(2^100) is the same double as log(nextafter(2^100, 1.0/0.0)), so any solution based on floating-point natural logarithms will fail.
Here's (nonconformant C++, not PHP) code for 4:
int ceillog2(unsigned long long x) {
if (x < 2) return x-1;
double d = x-1;
int ans = (long long &)d >> 52;
return ans - 1022;
}

php number format with several rules

given a number and n, I want to format it in the following way:
if it's an integer(without any decimal point), just return it
if it has x decimal point, only show m decimal point where m is the minimum of x and n.
number_format can't do this, what would be the easiest way to achieve this?
a few examples:
input: number = 60, n=1, output: 60
input: number=60.0, n=0, output: 60
input: number = 60.0623, n=1, output: 60.0
input: number = 60.06, n=3, output: 60.06
Try this:
$number = 12345;
$maxdecimals = 5;
echo rtrim(rtrim(number_format($number,$maxdecimals),"0"),".");
This will trim off trailing zeroes, and the decimal point if there are only zeroes following it. In this case, you'll get 12,345.
the answer is
round($number,$maxdecimals)

What does a leading zero do for a php int?

echo(073032097109032116104101032118101114121032109111100101108032111102032097032109111100101114110032109097106111114032103101110101114097108046);
Essentially, a very large number. Now, why does it output 241872? I know PHP has float handlers. When I remove the leading zero, it functions as expected. What is that leading zero signifying?
If you use a leading zero, the number is interpreted by PHP as an octal number. Thus, a 9 is not a valid part of the number and the parser stops there:
0730320 (base 8)
= 7 * 8^5 + 3 * 8^4 + 3 * 8^2 + 2 * 8^1
= 7 * 32768 + 3 * 4096 + 3 * 64 + 2 * 8 (base 10)
= 241872 (base 10)
A leading zero indicates an octal integer value
Manual:
Warning
If an invalid digit is given in an octal integer (i.e. 8 or 9), the rest of the number is ignored.
Example #2 Octal weirdness
<?php
var_dump(01090); // 010 octal = 8 decimal
?>
So your number gets cut off after 0730320, which is 241872 in decimal.
010 gives you 8 (octal number)
0x10 gives you 16 (hexadecimal number)

Categories