Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int main(void)
- {
- int integer; // number to be entered by user
- int i, b, n;
- unsigned int ones;
- printf("Please type in a decimal integern"); // prompt
- fflush(stdout);
- scanf("%d", &integer); // read an integer
- if(integer < 0)
- {
- printf("Input value is negative!"); // if integer is less than
- fflush(stdout);
- return; // zero, print statement
- }
- else{
- printf("Binary Representation:n", integer);
- fflush(stdout);} //if integer is greater than zero, print statement
- for(i = 31; i >= 0; --i) //code to convert inputted integer to binary form
- {
- b = integer >> i;
- if(b&1){
- printf("1");
- fflush(stdout);
- }
- else{
- printf("0");
- fflush(stdout);
- }
- }
- printf("n");
- fflush(stdout);
- ones = 0; //empty value to store how many 1's are in binary code
- while(integer) //while loop to count number of 1's within binary code
- {
- ++ones;
- integer &= integer - 1;
- }
- printf("Number of 1's in Binary Representation: %dn", ones); // prints number
- fflush(stdout); //of ones in binary code
- printf("Maximum Block Length: n");
- fflush(stdout);
- printf("n");
- fflush(stdout);
- return 0;
- }//end function main
- int maxRunLen(uint32_t num) {
- int count = 0;
- int maxCount = 0;
- while(num) {
- if(num & 1) count++;
- else {
- if( count > maxCount) maxCount = count;
- count = 0;
- }
- num >>=1;
- }
- if( count > maxCount) maxCount = count;
- return maxCount;
- }
- num & 1
- num >>= 1;
- num = num >> 1;
- int max_run_of_ones (unsigned x)
- {
- int max_run = 0;
- int cur_run;
- while (x != 0) {
- // skip right-most zeros
- while ((x & 1) == 0) {
- x >>= 1;
- }
- // skip and measure right-most run of ones
- cur_run = 0;
- while ((x & 1) == 1) {
- cur_run++;
- x >>= 1;
- }
- if (cur_run > max_run) max_run = cur_run;
- }
- return max_run;
- }
- import re
- number = 500
- binary_repr = bin(number)[2:] # '111110100'
- blocks = re.split(r'0+', binary_repr) # ['11111', '1', '']
- block_lengths = [len(x) for x in blocks] # [5, 1, 0]
- maximum_block_length = max(block_lengths) # 5
- import re
- number = 500
- binary_repr = bin(number)[2:] # '111110100'
- blocks = re.split(r'0+', binary_repr) # ['11111', '1', '']
- block_lengths = [len(x) for x in blocks] # [5, 1, 0]
- maximum_block_length = max(block_lengths) # 5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement