C Program to Find Next Higher Value with Same Number of Set Bits

This is a C Program to find next higher value of n with same 1’s.

Problem Description

This C Program next higher value of n with same 1’s.

Problem Solution

Take input from the user and perform bit operations as shown in the program below.

Program/Source Code

Here is source code of the C Program to next higher value of n with same 1’s. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C Program to next higher value of n with same 1's
 */
#define NUM_BITS_INT 32
#include <stdio.h>
#include <stdlib.h>
int newcount(int);
void main()
{
    int count1 = 0, k = 0, j, t, n, bit, i = 1, count = 0;
    printf("Enter a number : ");
    scanf("%d", &n);
    t = n;
 
    if (t == 0)
    {
        printf ("\nThe next highest number is : 1\n");
        exit (0);
    }
 
    while(t != 0)
    {
        bit = t & 0x80000000;
        if (bit == -0x80000000) 
        {
            bit = 1;  
        }
        if (bit == 1) 
            count++;
        t = t << 1;
 
    }
    for (k = n + 1;;k++)
    {
        count1 = newcount(k);    
        if (count1 == count)
        {
            printf("The next highest number is : %d ", k);
            break;
        }
    }
}
 
/* To count the no. of 1's in the no. */
int newcount(int k)
{
    int bit, count = 0;
    while (k != 0)
    {
        bit = k & 0x80000000;
        if (bit == -0x80000000)
        {
            bit = 1;
        }
        if (bit == 1)    
            count++;
        k = k << 1;
    }
    return(count);
}
Program Explanation

In this C Program, we are reading the number using ‘n’ variable. While loop is used to check that the value of ‘t’ variable is not equal to 0. If the condition is true, then execute the statement.

advertisement
advertisement

Binary AND operator is used to copy a bit to the ‘bit’ operator. If condition statement is used to check that the value of ‘bit’ variable is equal to -0x80000000. If the condition is true then execute the statement and assign the value of ‘bit’ variable as 1.

Another if condition statement is used to check that the value of ‘bit’ variable is equal to 1. If the condition is true, then execute the statement and increment the value of ‘count’ variable. Binary left shift operator is used for moving the number of bits value 1 to left by the number of bits specified by the value of ‘t’ variable and assign the value to ‘t’ variable.

For loop is used to print the next highest number. The newcount() function is used to count the number of 1’s in the number. While loop is used to check that the value of ‘k’ variable is not equal to 0. If the condition is true, then execute the statement. Binary AND operator is used to copy a bit to the ‘bit’ operator.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

If condition statement is used to check that ‘bit’ variable value is equal to -0x80000000. If the condition is true then execute the statement and assign the ‘bit’ variable value as 1. Another if condition statement is used to check that the value of ‘bit’ variable is equal to 1. If the condition is true, then execute the statement and increment the value of count variable.

Binary left shift operator is used for moving the number of bits 1 value to left by the number of bits specified by the ‘k’ variable value and assign the value to ‘k’ variable. If condition statement is used to check that both the values of ‘count1’ and ‘count’ variables are equal. If the condition is true then execute the statement and print the next highest value in the number with same 1’s.

Runtime Test Cases
 
$ cc bit18.c
$ a.out
Enter a number : 128
The next highest number is : 256 
Enter a number : 127
The next highest number is : 191
Enter a number : 6
The next highest number is : 9
Enter a number : 12
The next highest number is : 17

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.