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

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

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

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); }

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.

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.

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.

$ 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.**

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

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

**Related Posts:**

- Watch Advanced C Programming Videos
- Practice BCA MCQs
- Check C Books
- Check Computer Science Books
- Apply for C Internship