How to Convert Decimal Numbers to Binary - Flexiple (2024)

In this article, we take a look at what the decimal and binary number systems are, how to convert decimal numbers to binary, and vice versa. We also explain the math behind decimal-binary conversion and list simple programs in Java, JavaScript, and Python for converting decimal to binary numbers.

Background

The decimal and binary number systems are two of the most used numbers systems across the world. The decimal system is the most familiar to us, and is used almost everywhere in our day-to-day calculations. It has a base of 10 - i.e., 10 numerical characters, or 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9.

On the other hand, the binary number system is used extensively in machine-level programming. It has a base of 2, i.e., two numerical characters - 0 and 1.

Almost every computer and digital device is powered by the binary system, because it is very easy to implement in electronic circuits via logic gates. In computers, every digit of a binary number is called a 'bit'. For example:

  • 10 is a 2-bit binary number, where 1 and 0 are bits
  • 110 is a 3-bit binary number
  • 11011 is a 5-bit binary number

There are several methods and programs for converting decimal numbers to binary, and vice-versa. Let's first take a look at the most common method used for conversion, and then delve deeper into the math behind the decimal-binary conversion.

How to convert decimal numbers to binary

Let's look at two cases - converting decimal integers to binary, and then decimal fractions less than 1 to binary.

Decimal integers to binary: Algorithm

Step 1. Divide the integer by 2, while noting the quotient and remainder.

Step 2. Then, divide the quotient again by 2, and record the 3rd quotient and remainder.

Step 3. Like this, keep dividing each successive quotient by 2 until you get a quotient of zero.

Step 4. After this, just write all the remainders in reverse order to get the binary representation of the integer.

For example, let's convert the integer 15 to binary. Divide the integer by 2 successively while noting the quotient and remainder.

How to Convert Decimal Numbers to Binary - Flexiple (1)

Next, write the all the remainders in reverse order. In this example, remainders are 1, 1, 1, 1. So, writing in reverse order, the number 15 in decimal is represented as 1111 in binary.

For an even integer, let's take the example of 10.

How to Convert Decimal Numbers to Binary - Flexiple (2)

So, the decimal number 10 is represented as 1010 in binary.

Decimal fractions to binary: Algorithm

To convert a fraction to binary, follow the opposite of the process described above.

Step 1. Multiply the fraction by 2, while noting down the resulting integer and fraction parts of the product.

Step 2. Keep multiplying each successive resulting fraction by 2 until you get a resulting fraction product of zero.

Step 3. Now, write all the integer parts of the product in each step.

For example, let us convert the fraction 0.625 to binary. Multiply the fraction by 2 successively while noting the integer and fractional parts of the product.

How to Convert Decimal Numbers to Binary - Flexiple (3)

We see that the resulting integer parts of the product are 1, 0, 1. Hence, just write these after the decimal point to get the binary notation. So, 0.625 can be written as 0.101 in binary.

How to convert binary numbers to decimal

Let's look at two cases - converting binary integers to decimal, and then binary fractions to binary.

Binary integers to decimal: Algorithm

Step 1. To convert a binary integer to decimal, start by adding the left-most digit to 0.

Step 2. Next, multiply this by 2, and add the next digit in your number (as you progress from left to right) to this product. (In other words, multiply the current product in each step by 2 and add the current digit).

Step 3. Continue this process until there are no more digits left.

For example, let us convert the binary integer 1001 to decimal. Multiply the total by 2 and add the current digit, starting from the left.

How to Convert Decimal Numbers to Binary - Flexiple (4)

Hence, the binary to decimal conversion of 1001 is 9.

Binary fractions to decimal

Step 1. For converting a binary fraction to decimal, start by adding the right-most digit to 0.

Step 2. Next, divide this by 2, add the next digit to this total (while progressing from right to left), and so on.

Step 3. Keep doing this until there are no more digits left.

For example, let's convert the fraction 0.1101 to decimal. Divide the total by 2 and add the current digit, starting from the right.

How to Convert Decimal Numbers to Binary - Flexiple (5)

As seen, the decimal representation of 0.1101 is 0.8125.

The math behind decimal-binary conversion

Any number in a number system can be represented as its base expansion, where 'base' is the number base - eg., base-2 being binary, base-10 being decimal, base-16 being hexadecimal, and so on.

So, any integer can be represented as follows:

How to Convert Decimal Numbers to Binary - Flexiple (6)

where,

A is an integer
x is the digit value
b is the base value

For example, the integer 694 can be represented in base-10 and base-2 as:

How to Convert Decimal Numbers to Binary - Flexiple (7)

Similarly, a fraction can be as follows:

How to Convert Decimal Numbers to Binary - Flexiple (8)

where,

A is a fraction
x is the digit value
b is the base value

For example, the fraction 0. can be represented in base-10 and base-2 as:

How to Convert Decimal Numbers to Binary - Flexiple (9)

Binary representation of non-terminating and recurring numbers

Some fractions that can be represented in the decimal system run into an infinite stream of 0's and 1's. These are the fractions that when converted to decimals in the decimal system, return an infinitely-long stream of digits after the period. Hence, they're called non-terminating and recurring numbers.

For example, consider the fraction 1/6. The decimal value of 1/6 is:

How to Convert Decimal Numbers to Binary - Flexiple (10)

We see that the digit '6' keeps recurring after the period, unto infinity.

This is because, to finitely represent any given number in a base-n system, the denominator of that number must be a power of the base (b). In the above example, 6 is not a power of 10, so 1/6 can't be represented in the base-10 system in decimal format.

Similarly, those numbers with a denominator that are not a power of 2 can't be represented finitely in the base-2 system.

For example, consider 0.3. 0.3 = 3/10. Since 10 is not a power of 2, 0.3 can't be represented in the base-2 system.

How to Convert Decimal Numbers to Binary - Flexiple (11)

We see that the 4 digits '1001' keep repeating after the decimal point to infinity. Hence, we can represent it as:

How to Convert Decimal Numbers to Binary - Flexiple (12)

Decimal to binary table

Here's a handy decimal-binary conversion table:

How to Convert Decimal Numbers to Binary - Flexiple (13)

Programs for converting decimal to binary

Here's an example of programs for converting decimal numbers to binary numbers. The logic used is mostly the same for both - Java and Python.

Decimal to binary in Java

// How to convert a decimal number to binary// Java programimport java.io.*;class decimal-to-binary { static void decimaltobinary(int x) { int[] binary = new int[32]; int i = 0; while (x > 0) { binary[i] = x % 2; x = x / 2; i++; } for (int j = i - 1; j >= 0; j--) System.out.print(binary[j]); }// driver public static void main(String[] args) { int x = 17; decimaltobinary(x); }}

Decimal to binary in JavaScript

<script>// How to convert a decimal number to binary// Javascript program// function for conversionfunction decimaltobinary(x){ let binary = new Array(32); let i = 0; while (x > 0) { binary[i] = x % 2; x = Math.floor(x / 2); i++; } for (let j = i - 1; j >= 0; j--) document.write(binary[j]);}// driver let n = 17; decimaltobinary(x); </script>

Decimal to binary in Python

# How to convert a decimal number to binary# Python program# function for conversiondef decimaltobinary(x): binary = [0] * x; i = 0; while (x > 0): binary[i] = x % 2; x = int(x / 2); i += 1; for j in range(i - 1, -1, -1): print(binary[j], end = "");# driverx = 17;decimaltobinary(x);
How to Convert Decimal Numbers to Binary - Flexiple (2024)
Top Articles
Re- CBN Spends N58.6 billion to print notes – 9News Nigeria
Introduction to Derivatives–Options, Futures, and Others | AnalystPrep - FRM Part 1
Drury Inn & Suites Bowling Green
Usborne Links
Geodis Logistic Joliet/Topco
Sprague Brook Park Camping Reservations
2022 Apple Trade P36
Free Robux Without Downloading Apps
House Share: What we learned living with strangers
You can put a price tag on the value of a personal finance education: $100,000
2135 Royalton Road Columbia Station Oh 44028
How to Store Boiled Sweets
Colts Snap Counts
Conan Exiles Thrall Master Build: Best Attributes, Armor, Skills, More
Teenleaks Discord
Samantha Lyne Wikipedia
Gem City Surgeons Miami Valley South
Osborn-Checkliste: Ideen finden mit System
Google Doodle Baseball 76
north jersey garage & moving sales - craigslist
Used Safari Condo Alto R1723 For Sale
2021 Volleyball Roster
C&T Wok Menu - Morrisville, NC Restaurant
Inbanithi Age
Culver's.comsummerofsmiles
Anesthesia Simstat Answers
Viduthalai Movie Download
Basil Martusevich
Smayperu
Palmadise Rv Lot
Metra Union Pacific West Schedule
Truis Bank Near Me
John F Slater Funeral Home Brentwood
The Mad Merchant Wow
4083519708
Western Gold Gateway
To Give A Guarantee Promise Figgerits
How To Get Soul Reaper Knife In Critical Legends
Claim loopt uit op pr-drama voor Hohenzollern
Craigslist Putnam Valley Ny
Gary Lezak Annual Salary
Colorado Parks And Wildlife Reissue List
Union Corners Obgyn
Umiami Sorority Rankings
Lake Kingdom Moon 31
Santa Clara County prepares for possible ‘tripledemic,’ with mask mandates for health care settings next month
Comanche Or Crow Crossword Clue
Tropical Smoothie Address
Minterns German Shepherds
Assignation en paiement ou injonction de payer ?
Grace Charis Shagmag
Jasgotgass2
Latest Posts
Article information

Author: Jamar Nader

Last Updated:

Views: 6832

Rating: 4.4 / 5 (55 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Jamar Nader

Birthday: 1995-02-28

Address: Apt. 536 6162 Reichel Greens, Port Zackaryside, CT 22682-9804

Phone: +9958384818317

Job: IT Representative

Hobby: Scrapbooking, Hiking, Hunting, Kite flying, Blacksmithing, Video gaming, Foraging

Introduction: My name is Jamar Nader, I am a fine, shiny, colorful, bright, nice, perfect, curious person who loves writing and wants to share my knowledge and understanding with you.