decbin() - 十进制转换为二进制 - php 数学函数
decbin()
(PHP 4, PHP 5, PHP 7)
十进制转换为二进制
说明
decbin(int $number): string返回一字符串,包含有给定$number参数的二进制表示。所能转换的最大数值为十进制的 4294967295,其结果为 32 个 1 的字符串。
参数
$numberDecimal value to convert
positive$number | negative$number | return value |
---|---|---|
0 | 0 | |
1 | 1 | |
2 | 10 | |
... normal progression ... | ||
2147483646 | 1111111111111111111111111111110 | |
2147483647 (largest signed integer) | 1111111111111111111111111111111 (31 1's) | |
2147483648 | -2147483648 | 10000000000000000000000000000000 |
... normal progression ... | ||
4294967294 | -2 | 11111111111111111111111111111110 |
4294967295 (largest unsigned integer) | -1 | 11111111111111111111111111111111 (32 1's) |
positive$number | negative$number | return value |
---|---|---|
0 | 0 | |
1 | 1 | |
2 | 10 | |
... normal progression ... | ||
9223372036854775806 | 111111111111111111111111111111111111111111111111111111111111110 | |
9223372036854775807 (largest signed integer) | 111111111111111111111111111111111111111111111111111111111111111 (63 1's) | |
-9223372036854775808 | 1000000000000000000000000000000000000000000000000000000000000000 | |
... normal progression ... | ||
-2 | 1111111111111111111111111111111111111111111111111111111111111110 | |
-1 | 1111111111111111111111111111111111111111111111111111111111111111 (64 1's) |
返回值
Binary string representation of$number
范例
Example #1decbin()例子
以上例程会输出:
1100 11010
参见
bindec()
二进制转换为十进制decoct()
十进制转换为八进制dechex()
十进制转换为十六进制base_convert()
在任意进制之间转换数字printf()
输出格式化字符串,using%b,%032bor%064bas the formatsprintf()
,using%b,%032bor%064bas the format
To add leading zeros I prefer the following:
A fast function to convert a binary string to a bit sequence
Regarding trailing zeros, after test all the option mention here by others, i have performed my own tests regarding efficiency, here are the results: results 0000100100001001000010010000100.... (output is echoed 1000 times) Duracion 0.0134768486023 segundos 8 Duracion 0.00054407119751 segundos 00001001 Duracion 0.000833988189697 segundos 00001001 Thus the winner is
Print as binary format with leading zeros into a variable in one simple statement.
Another larger-than-31-bit function. Works for very large numbers, but at the expense of perfect bit-precision as the size increases (I noticed rounding errors past 16 or so decimal places) so use with caution, and only when decbin() won't cut it. function Dec2Bin($number) { while ($number >= 256) { $bytes[] = (($number / 256) - (floor($number / 256))) * 256; $number = floor($number / 256); } $bytes[] = $number; for ($i=0;$i
A little useful little function that returns a binary string with leading 0s: function d2b($n) { return str_pad(decbin($n), 16, "0", STR_PAD_LEFT); } // example: echo d2b(E_ALL); echo d2b(E_ALL | E_STRICT); echo d2b(0xAA55); echo d2b(5); Output: 0111011111111111 0111111111111111 1010101001010101 0000000000000101
This is my System: You can convert a decimal number to a number system you want, like the binary system.
Careful trying binary-wise tests with integers: # FFFFFFFF command: php -r 'print(decbin(4294967295)."\n");' result: 11111111111111111111111111111111 # C3E9CAC8 command: php -r 'print(decbin(3286878920)."\n");' result: 11000011111010011100101011001000 # regardless of specifying "(int)", using bitwise AND: command: php -r 'print((int)(3286878920 & 4294967295)."\n");' result: -1008088376 (int) # now the expected result will happen (guess the performance impact) command: php -r 'print(bindec(decbin((3286878920 & 4294967295)))."\n");' result: 3286878920 (float) additional note: if you "bitwise and" some random bits with a sequence of 1-bit of the same length, the expected result is the same "random bits sequence" unchanged. If you want to keep this in the integer world for faster comparisons, you risk messing your result for the signed integer size limitation. The maximum value you can use for the desired result is (7FFFFFFF -- or integer 2147483647), half of the maximum 'unsigned' integer 32-bit(platform-dependent) value.
I think this is the best function. Is almost endlessy (till 2^50 or something)
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)