In: Computer Science
ConvertingDecimalValuesintoBina ry,andViceVersa. PartA Being
able to convert decimal values to binary (and vice versa) is very
important in networking because this is the basis for how
subnetting is done. You may have done some
of these exercises in high school and probably didn’t know why it
was important to be able to convert decimal values into binary, and
vice versa. This hands-on activity will help
yourecallhowthisisdoneorwillteachhowtodoitincase
youneverseenthisbefore.
152 Chapter 5 Network and Transport Layers
As you know, an IPv4 address consists of 32 bits that have been
separated into 4 bytes (sometimes called octets), for example,
129.79.126.1. This is called the dotted decimal address. Each byte
has 8 bits, and each of these bits can
assumeavalueof0or1.Thefollowingtableshowshowwe
converteachbinarypositiontoadecimalvalue:
Binaryposition 27 26 25 24 23 22 21 20 Decimalvalue 128 64 32 16 8
4 2 1
To practice the conversion from binary to decimal, let’s
doacoupleproblemstogether: 1. You have the following binary number:
10101010. Convertitintodecimal. 10101010=(1 ∗ 128)+(0 ∗ 64)+(1 ∗
32) +( 0 ∗ 16)+(1 ∗ 8)+(0 ∗ 4) +( 1 ∗ 2)+(0 ∗ 1)=128 +31+8+2 = 170
2. You have the following binary number: 01110111.
Convertitintodecimal. 01110111=(0×128)+(1 ∗ 64)+(1 ∗ 32) +( 1 ∗
16)+(0 ∗ 8)+(1 ∗ 4) +( 1 ∗ 2)+(1 ∗ 1) =64+32+16+4+2+1 = 119
Itisimportanttonoticewhattherangeofpossibledecimal values for each
byte is. The lower bound is given when each bit is 0 and the upper
bound is when each bit is 1. So
00000000willgiveus0and11111111willgiveus255.This
isthereasonwhyIPv4addressescannotgoabovethevalue of255. Deliverable
Calculate the decimal values of the following binary
numbers:11011011,01111111,10000000,11000000,11001101. PartB
Nowlet’spracticetheconversionofdecimalvaluetobinary. This is a bit
trickier. Start by finding the highest binary
position that is equal to or smaller than the decimal number we are
converting. All the other placeholders to the left of this number
will be 0. Then subtract the placeholder value from the number.
Then find the highest binary position that is equal to or smaller
than the remainder. Keep repeating these steps until the remainder
is 0. Now, let’s practice. 3. Convert60intoabinarynumber. a.
Theplaceholderthatisequaltoorlowerthan60is
32.Therefore,thefirsttwobitsfor60are0andthe third one is 1 − 001_ _
_ _ _ . The next step is to subtract32from60,whichequals60−32 = 28.
b. The placeholder that is equal to or lower than 32 is 16, which
is the fourth bit from the left. Therefore, our binary number will
look like this: 0011_ _ _ _. The next step is to subtract 16 from
28,whichequals28−16 = 12. c.
Theplaceholderthatisequaltoorlowerthan12is 8, and this is the fifth
bit from the left. Therefore,
ourbinarynumberwilllooklikethis:00111___.
Thenextstepistosubtract8from12,whichequals 12−8 = 4. d. The
placeholder that is equal to or lower than 4 is 4,
andthisisthesixth bitfromtheleft. Therefore, our binary number will
look like this: 001111_ _.
Thenextstepistosubtract4from4,whichequals 4−4 = 0. e. Given that
our remainder is 0, the additional bits
are0,andwefindthatouranswer:60inbinaryis 00111100. 4.
Convert182intoabinarynumber. 182=10110110 (Because182−128 =
54,54−32 = 22,22−16 = 6, and6−4 = 2)
Deliverable Calculate the binary value for each of the following
binary numbers:126,128,191,192,223
Example , below is the method to convert decimal values to binary efficiently.
(12) base 10
Step 1)
12%2=0
12/2=6
Step 2)
6%2=0
6/2=3
Step-3)
3%2=1
3/2=1
Step 4)
1%2=1
1/2=0
Stop
compute result from behind
So binary value is (1100) base 2 for decimal value 12
Same steps are follwed in code to generate all answers asked.
Code:
#include <bits/stdc++.h>
using namespace std;
void ConvertToBinary(int decimal)
{
int arr[32];
int i = 0;
// till value is greater than 0, keep taking modulo
2
while (decimal > 0) {
arr[i] = decimal % 2;
decimal = decimal / 2;
i++;
}
// print elements from right to left
for (int k = i - 1; k >= 0; k--)
cout << arr[k];
cout<<"\n";
}
int main()
{
// main method for fetching results
int t=6;
while(t--){
int n;
cin>>n;
ConvertToBinary(n);
}
return 0;
}
Screenshot :
Input:
Output: