Question

In: Computer Science

ConvertingDecimalValuesintoBina ry,andViceVersa. PartA Being able to convert decimal values to binary (and vice versa) is very...

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

Solutions

Expert Solution

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:


Related Solutions

Convert the decimal number, 315.56 into binary form?
Convert the decimal number, 315.56 into binary form?
i need to convert decimal to twos complement binary and then add the binary digits but...
i need to convert decimal to twos complement binary and then add the binary digits but I am unable to do it. I am only allowed to use string, can anyone help me out please. i need the code urgently. #include #include #include #include using namespace std; string reverse(string s) { string x = ""; for (long i = s.length() - 1; i >= 0; i--) { x += s[i]; } return x; } string twosComplementStringsAddition(string A, string B) {...
Convert the following binary number to dotted decimal format. 11000000100110010000100101011001
Convert the following binary number to dotted decimal format. 11000000100110010000100101011001
1. Explain how to convert positive integers from base 3 to base 10 and vice versa....
1. Explain how to convert positive integers from base 3 to base 10 and vice versa. Include examples. 2. Explain how to convert fractions from base 3 to base 10 and vice versa. Include examples.
Write a program to convert the time from 24-hour notation to 12-hour notation and vice versa....
Write a program to convert the time from 24-hour notation to 12-hour notation and vice versa. Your program must be menu driven, giving the user the choice of converting the time between the two notations. Furthermore, your program must contain at least the following functions: a function to convert the time from 24-hour notation to 12-hour notation; a function to convert the time from 12-hour notation to 24-hour notation; a function to display the choices; function(s) to get the input;...
1) Covert the following binary values to decimal. Do this interpreting the binary as unsigned and...
1) Covert the following binary values to decimal. Do this interpreting the binary as unsigned and signed. a. 0111 1001 b. 1000 0000 c. 1111 1111 PLEASE EXPLAIN IT IN DETAIL
Convert from rectangular to polar form and vice versa: a. ?6 b. 2√5∠135∘ c. −2 −...
Convert from rectangular to polar form and vice versa: a. ?6 b. 2√5∠135∘ c. −2 − ?2√3 d. √2∠45∘ Please show your steps because I need to understand how we reached the solution Thank you
Trade tariffs being levied by the United States against China & vice versa occupy a great...
Trade tariffs being levied by the United States against China & vice versa occupy a great deal of the news presently. With your knowledge of economics, you are able to analyze the direct and indirect impacts of tariffs on U.S. commercial enterprises. Take a position on the tariffs. Choose whether you are in favor of or opposed to the tariffs imposed by the trump administration on Chinese products sold in the United States. Supporting your position by using economic reasoning...
(6 pts) Convert the data representation given below • Convert 10110111 unsigned binary representation, to decimal...
(6 pts) Convert the data representation given below • Convert 10110111 unsigned binary representation, to decimal representation. • Convert 01100000101110000001010111111000 the binary representation to a hexadecimal representation. • Convert 0xBAAADA55 hexadecimal representation, to a binary representation. 2. (8 pts) Complete the following arithmetic operations in two’s complement representation. What are the values of the carry flag and the overflow flag? (Assume a six-bit system) • 31 + 11 • 13 – 15 • (-2) x (-16) • (-15) ÷ 5
Convert these numbers from Decimal to Binary 111: 66: 252 11 20 Convert these numbers from...
Convert these numbers from Decimal to Binary 111: 66: 252 11 20 Convert these numbers from Binary to Decimal 00110110 11111000 00000111 10101010 What is the Default Subnet mask of Class A IPV4 What is the Default Subnet mask of Class B IPV4 What is the Default Subnet mask of Class C IPV4 What is the CIDR notation or / short handwriting of Subnet masks: Class A: /?. Explain the reason Class B: /? Explain the reason Class C: /?...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT