Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 wont 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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke, David J. Auer

7th edition

133544621, 133544626, 0-13-354462-1, 978-0133544626

More Books

Students also viewed these Databases questions