Mathematics7 min readExamples run on PHP 8.5.10

decbin()

Learn how PHP decbin() turns an integer into a string of binary digits, how negative numbers behave, and how to pad or mask results.

Signature

decbin(int $num): string

Purpose

decbin() converts an integer into a string made of the characters 0 and 1. It is handy when you want to see which bits of a number are set, when you debug flag masks, or when you need to show a value in base 2 for teaching or logging. The name stands for "decimal to binary", although the argument is simply a PHP integer, not a decimal-formatted string.

Signature

Parameters and defaults

$num is the only parameter and it has no default. It is the integer you want to convert. Because the declared type is int, ordinary weak-typing rules apply: an integer-like value such as "12" is accepted in normal mode, while a string such as "abc" raises a TypeError. With declare(strict_types=1) only real integers are accepted.

Return value and failure cases

The function always returns a string. For positive numbers the result has no leading zeros and no 0b prefix, so decbin(12) is "1100". Zero gives "0". There is no false return and no warning for out-of-range values; every valid integer produces some bit string.

Negative numbers are not shown with a minus sign. Instead you get the two's complement bit pattern of the integer at the machine's native integer width. On a 64-bit build that means -1 becomes 64 ones, and -2 becomes 63 ones followed by a zero. On a 32-bit build the same values give 32 characters. Code that must behave the same everywhere should not depend on the exact length of a negative result.

Example 1: basic conversion

Example
<?php
echo decbin(12) . "\n";
echo decbin(26) . "\n";
echo decbin(0) . "\n";
Output
1100
11010
0

Each call returns a string, so concatenating a newline is straightforward. Notice that zero is a single character rather than an empty string.

Example 2: negative numbers

Example
<?php
$negative = decbin(-2);
echo json_encode(strlen($negative) === PHP_INT_SIZE * 8) . "\n";
echo substr($negative, -4) . "\n";
echo substr_count(decbin(-1), "1") === PHP_INT_SIZE * 8 ? "all ones" : "unexpected";
echo "\n";
Output
true
1110
all ones

The length of a negative result equals the number of bits in a native integer, which is PHP_INT_SIZE * 8. Comparing against that constant keeps the example correct on any build. The last four characters of -2 are 1110, and -1 consists only of ones.

Example 3: fixed-width output

Example
<?php
function toByte(int $n): string {
    return str_pad(decbin($n & 0xFF), 8, "0", STR_PAD_LEFT);
}
echo toByte(5) . "\n";
echo toByte(255) . "\n";
echo toByte(-1) . "\n"; echo sprintf("%08b", 5) . "\n";
Output
00000101
11111111
11111111
00000101

This helper masks the value to its lowest eight bits with & 0xFF, converts it, and left-pads with zeros. Masking also tames negative input: -1 & 0xFF is 255, so the output is eight ones. The final line shows that sprintf() with %08b achieves the same padding in one step for non-negative input.

Edge cases

  • Zero returns "0", never an empty string.
  • No padding is added. decbin(5) is "101", not "00000101", so pad it yourself when you need aligned columns.
  • Largest integer PHP_INT_MAX yields a string of ones one bit shorter than the native width (63 characters on a 64-bit build), because the sign bit is zero.
  • Smallest integer PHP_INT_MIN yields a one followed by zeros across the full native width.
  • Floats are not a good input. A float is not an integer type, so convert deliberately with a rounding function of your choice before calling decbin().

Common mistakes

  1. Expecting a minus sign. decbin(-5) does not return "-101". If you want a signed representation, use abs() and prepend the sign yourself.
  2. Assuming a fixed length. Positive results grow with the value, so comparing strings as if they were aligned will give wrong answers.
  3. Treating the result as a number. The return value is a string. Doing arithmetic on it, or comparing it with == against an integer such as 101, mixes up binary text with decimal values.
  4. Passing binary text in. decbin("1010") converts the decimal number one thousand and ten, not the binary pattern. Use bindec() to go the other direction.
  5. Relying on platform width. Negative results differ between 32-bit and 64-bit builds.
  • bindec() converts a binary string back to a decimal number.
  • decoct() and dechex() produce octal and hexadecimal strings.
  • base_convert() converts between arbitrary bases using strings.
  • sprintf() and printf() with %b, %032b or %064b format binary output with padding.

Recap

decbin(int $num): string returns the base-2 text for an integer. Positive values have no leading zeros, zero is "0", and negative values show their two's complement pattern at native integer width. Pad with str_pad() or sprintf() for fixed widths, mask with & when you need a specific bit count, and use bindec() to reverse the conversion.

Tested examples

Basic conversion of positive integers and zero
<?php
echo decbin(12) . "\n";
echo decbin(26) . "\n";
echo decbin(0) . "\n";
Output
1100
11010
0
Negative numbers use the native-width two's complement pattern
<?php
$negative = decbin(-2);
echo json_encode(strlen($negative) === PHP_INT_SIZE * 8) . "\n";
echo substr($negative, -4) . "\n";
echo substr_count(decbin(-1), "1") === PHP_INT_SIZE * 8 ? "all ones" : "unexpected";
echo "\n";
Output
true
1110
all ones
Fixed-width byte output with masking and padding
<?php
function toByte(int $n): string {
    return str_pad(decbin($n & 0xFF), 8, "0", STR_PAD_LEFT);
}
echo toByte(5) . "\n";
echo toByte(255) . "\n";
echo toByte(-1) . "\n"; echo sprintf("%08b", 5) . "\n";
Output
00000101
11111111
11111111
00000101