To find the maximum number of consecutive bits

Source: Internet
Author: User

Title Description

Function: To find a byte number corresponding to the binary number 1 of the maximum consecutive number, such as 3 of the binary 00000011, the maximum continuous 2 1
Input: A byte-type number
Output: None
Returns: The maximum number of consecutive digits in the corresponding binary number 1

Enter a description

Enter a byte number

Output description

The number of consecutive 1 after the output turns into binary

Input sample  

3

Output sample

2

Problem analysis

You can find the maximum number of consecutive 1 for the right edge of the digital edge.

Test code

1#include <stdio.h>2 3 intMainvoid)4 {5     intnum, Count =0, max =0;6 7      while(SCANF ("%d", &num)! =EOF)8     {9Count = max =0;Ten          while(num) One         { A             if(Num &1) -             { -count++; theMax = (Count > Max)?Count:max; -             } -             Else -             { +Count =0; -             } +Num >>=1; A         } atprintf"%d\n", max); -     } -     return 0; -}

To find the maximum number of consecutive bits

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.