Binary integer to decimal, binary integer to decimal

Source: Internet
Author: User

Binary integer to decimal, binary integer to decimal
I want to solve the problem of OJ, But I can use bitwise operations to calculate it. but note that the numeric constants in C language may overflow when the default type is int displacement.

 

 

Description

Returns a binary non-negative integer x, x <232, which is converted to a decimal number.

 

 

Input is multiple rows. Each row has a binary non-negative integer x. Output returns the decimal value corresponding to x in each row. Sample Input0 1 01 10 11 100001 1111111111111111 Sample Output0 1 1 2 3 33 65535 HINT

Pay attention to the data range !!!

 

1 # include "stdio. h "2 # include" string. h "3 const int maxn = 100000; 4 int main (int argc, char const * argv []) 5 {6 char indata [maxn]; 7 memset (indata, 0, sizeof (indata); 8 while (scanf ("% s", indata )! = EOF) 9 {10 int n = strlen (indata); 11 unsigned long sum = 0; 12 for (int I = n-1; I> = 0; I --) 13 {14 unsigned long k = indata [I]-'0'; 15 sum + = (k <(n-i-1 )); 16} 17 printf ("% llu \ n", sum); 18} 19 return 0; 20}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.