Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

ilihub profile imageharshraj8843
harshraj8843 forilihub

Posted on • Originally published atilihub.tech

How to Convert Binary to Decimal

Convertingbinary todecimal is a common task in computer programming. Binary is abase-2 number system that uses only two digits, 0 and 1. Decimal, on the other hand, is abase-10 number system that uses ten digits, 0 through 9. Converting binary to decimal involves multiplying each digit of the binary number by its corresponding power of 2 and adding up the results.

Conversion

Here's a step-by-step guide on how to convert binary to decimal:

  1. Choose the binary number you want to convert to decimal.
  2. Write down the powers of 2 from right to left, starting with202^0.
  3. Multiply each digit of the binary number by its corresponding power of 2.
  4. Add up the results from step 3 to get the decimal value.

Example

Let's walk through an example to illustrate the process. Suppose we want to convert the binary number1011 to decimal.

  1. The binary number we want to convert is 1011.
  2. The powers of 2 from right to left are202^0,212^1,222^2, and232^3
  3. We multiply each digit of the binary number by its corresponding power of 2. The results are:1×20=11 \times 2^0 = 1,1×21=21 \times 2^1 = 2,0×22=00 \times 2^2 = 0, and1×23=81 \times 2^3 = 8
  4. We add up the results from step 3 to get the decimal value.
1+2+0+8=111 + 2 + 0 + 8 = 11

Therefore, the decimal value of the binary number 1011 is 11.

Binary-Decimal Table

Here's a table that shows the decimal values of the first 16 binary numbers:

BinaryDecimal
00000
00011
00102
00113
01004
01015
01106
01117
10008
10019
101010
101111
110012
110113
111014
111115

Sample Code

This coderequires@ilihub/code npm package to run.

import{BinaryToDecimal}from"@ilihub/code";constbinary="10000000";constdecimal=BinaryToDecimal(binary);console.log(decimal);// Output// 128
Enter fullscreen modeExit fullscreen mode

References


Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Please take a moment to review ourlatest and revised blog post available on our official website.

More fromilihub

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp