The language of acomputer, called machine language, is a sequence of 0s and 1s. When you press the
The language of acomputer, called machine language, is a sequence of 0s and 1s. When you press the key A on the keyboard, 01000001 is stored in the computer. Also, the collating sequence of A in the ASCII character set is 65. In fact, the binaryrepresentation of A is 01000001 and the decimal representation of A is 65.The numbering system we use is called the decimal system, or base 10system. The numbering system that the computer uses is called the binarysystem, or base 2 system. The purpose of this exercise is to write a function to convert a number from base 2 to base 10.To convert a number from base 2 to base 10, we first find the weight of eachbit in the binary number. The weight of each bit in the binary number is assigned from right to left. The weight of the rightmost bit is 0. The weight of the bit immediately to the left of the rightmost bit is 1, the weight of thebit immediately to the left of it is 2, and so on. Consider the binary number1001101. The weight of each bit is as follows:
weight 6 5 4 3 2 1 0
1 0 0 1 1 0 1
We use the weight of each bit to find the equivalent decimal number. For each bit, we multiply the bit by 2 to the power of its weight, and then weadd all of the numbers. For the binary number 1001101, the equivalent
decimal number is
1 x 26+ 0 x 25+ 0 + 24+1 x23x1 x22+0 x21+1 x20
=64 +0 +0 +8 +4 +0 +1 =77
To write a program that converts a binary number into the equivalent decimal number, we note two things: (1) The weight of each bit in thebinary number must be known, and (2) the weight is assigned from right toleft. Because we do not know in advance how many bits are in the binary number, we must process the bits from right to left. After processing a bit, we can add 1 to its weight, giving the weight of the bit immediately to itsleft. Also, each bit must be extracted from the binary number and multipliedby 2 to the power of its weight. To extract a bit, you can use the modoperator. Write a program that uses a stack to convert a binary number into an equivalent decimal number and test your function for the following
values: 11000101, 10101010, 11111111, 10000000, 1111100000.
![](https://i0.wp.com/www.usaessays.com/wp-content/uploads/2021/07/bottom-of-every-post-15.jpg?ssl=1)