1. Engineering
  2. Computer Science
  3. input an integer containing only the digits 0 and 1...

Question: input an integer containing only the digits 0 and 1...

Question details
  • Input an integer containing only the digits 0 and 1 (i.e., a binary number) and print its decimal equivalent.
  • Your program should be able to handle input from 1 to 30 digits.
  • You do not need to check the input string for validity (i.e, if all characters entered are 0s or 1s); you can assume that the data will be valid. All input will consist of 0s and 1s and will be 1 or more characters long but not longer than 30 characters.
  • You may assume that all input will be valid; i.e., all digits will be either 0 or 1 and all input will be atleast 1 character and no longer than 30 characters. Because of these assertions, you do not need to doany data validation on the input.
  • Try to think in Perl, not C/C++/Java for this assignment. It will make the solution much easier and, for very large numbers, you probably won’t get the correct solution if you attempt to use a C/C++/Java solution.
  • If you're not sure what the binary number system is or it's been awhile since you last used it, you might try one of the following web sites for more information: http://www.webopedia.com/TERM/B/binary.html
  • Remember to have #!/usr/bin/env perl
  • Use use Modern::Perl; in your program.
  • Try to follow the Perl Best Practices
  • Don't use the pack/unpack functions of Perl; though they make the solution incredibly short and elegant, we're shooting for some experience dealing with breaking data into pieces and processing it while trying to avoid uninitialized values that you may create and encounter.

Example

Please enter a binary number up to 30 digits: 11111111111

11111111111 is 2047 in decimal.

Example

Please enter a binary number up to 30 digits: 11111111111111111111

11111111111111111111 is 1048575 in decimal.

Example

Please enter a binary number up to 30 digits: 00000000000000000001

00000000000000000001 is 1 in decimal.

Example

Please enter a binary number up to 30 digits: 1

1 is 1 in decimal.

Example

Please enter a binary number up to 30 digits: 10101010101010101010

10101010101010101010 is 699050 in decimal.

Example

Please enter a binary number up to 30 digits: 111111111111111111111111111111

111111111111111111111111111111 is 1073741823 in decimal.

Please enter a binary number up to 30 digits: 111111111111111111111111111110

111111111111111111111111111110 is 1073741822 in decimal.

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution