Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk1 min

Python bin() Function: Convert Integers to Binary

Python’s bin() converts integers to binary strings with a 0b prefix. Learn how to control the prefix, interpret negative results, and check bit length or set bits.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use bin(value) to convert an integer to a binary string: bin(10) returns '0b1010'. The 0b prefix marks the result as binary; use binary formatting when you want to omit it. Negative values get a minus sign, not a fixed-width two’s-complement representation.

Convert an integer with bin()

The built-in bin() function converts an integer to a binary string prefixed with 0b. The result is a valid Python expression. For example, bin(3) returns '0b11', and bin(10) returns '0b1010'. Python 3.14 Built-in Functions documentation

bin(3)   # '0b11'
bin(10)  # '0b1010'

The argument can be an int or another object that implements __index__() and returns an integer. It is positional-only, so call it as bin(value), not bin(number=value). A float such as 3.5 is not accepted.

Omit or keep the 0b prefix

Use the binary format code b to get just the digits. Add # before b to include the prefix. Both format() and f-strings support these options. Python 3.14 Built-in Functions documentation

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
format(14, 'b')   # '1110'
format(14, '#b')  # '0b1110'
f'{14:b}'         # '1110'
f'{14:#b}'        # '0b1110'

What happens with negative integers?

bin(-10) returns '-0b1010': the minus sign is followed by the binary digits for the magnitude. This is not a fixed-width two’s-complement representation. bin() does not select a width or signedness for you. If you need a fixed-width value for a byte or protocol, define the required width and signedness and encode the value accordingly. Python’s documentation points to enum.bin() for representing negative values as two’s complement. Python 3.14 Built-in Functions documentation

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Check an integer’s binary length or number of set bits

Python integers have unlimited precision. Use bit_length() to find how many binary digits are needed for the magnitude, excluding the sign and leading zeros. Use bit_count() to count the ones in the binary representation of the absolute value. Python 3.14 Built-in Types documentation

(-37).bit_length()  # 6
19 .bit_count()     # 3

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.