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?
Convert the following binary values to hexadecimal and decimal (1 pt each) Write Hex Numbers as...
Convert the following binary values to hexadecimal and decimal (1 pt each) Write Hex Numbers as 0x##(ex 0x0A, 0xFF) Binary Hexadecimal Decimal 0001-1011 0x 0000-1000 0000-0100 0000-1001 0001-1111 1001-1001 0111-1010 1100-0010 1110-0101 1000-1010 0011-0100 0001-1001 0100-0011 1111-1111 1110-0111 0001-0010 0100-1000 0100-1110 1001-0001 0110-1100 Name: Convert the following hexadecimal values to binary and decimal Write binary numbers as 0000-0000 Hexadecimal Binary Decimal 0xf1 0xac 0x56 0x6c 0x32 0x30 0x05 0x28 0xf0 0x07 0x42 0xb9 0x6d 0x2f 0x71 0x0e 0x2d 0xfb 0xba...
Convert the following values into binary numbers for each word and place the binary values in...
Convert the following values into binary numbers for each word and place the binary values in the two-dimensional array in their proper order of words. Value Binary Number Equivalent Word 0 462,91210 Word 1 1142008 Word 2 5420h Word 3 20,992d Word 4 1104208 Word 5 6102008 Word 6 9F88h Word 7 20,49610 Word 8 502416 Word 9 1101018 Word 10 71082h
Problem: Convert the following binary number to decimal. 1. 110101.101 Problem: Convert the following decimal number...
Problem: Convert the following binary number to decimal. 1. 110101.101 Problem: Convert the following decimal number to fractional binary representation. 1. 103.5625
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;...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT