 Tags IOS SQL HTML C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 # Finding a numbers with the largest/smallest number of bits set

By : Harshit Sinha
Date : September 23 2020, 05:00 PM
will be helpful for those in need Try to find the highest power of 2 that is within the range. This can be done by taking the upper bound and ANDing it with itself minus one in a loop, like: code :
``````temp = upper_bound;
while( (temp & (temp-1)) != 0) {
temp = temp & (temp-1);
}

if(temp < lower_bound) {
// No power of 2 found within range
}
``````
``````void find_bits(unsigned int lower_bound, unsigned int upper_bound, unsigned int extra_bits) {
unsigned int temp = upper_bound;
while( (temp & (temp-1)) != 0) {
temp = temp & (temp-1);
}

if(temp < lower_bound) {
find_bits(lower_bound ^ temp, upper_bound ^ temp, extra_bits + 1);
return;
}
``````
``````    if(lower_bound == 0) {
least_set_bits = 0 + extra_bits;
} else {
least_set_bits = 1 + extra_bits;
}
``````
``````    if(temp - 1 < lower_bound) {
most_set_bits = least_set_bits;
} else {
temp--;
most_set_bits = 0;
while(temp > 0) {
temp >>= 1;
most_set_bits++;
}
most_set_bits += extra_bits;
}
``````
``````void find_bits(unsigned int lower_bound, unsigned int upper_bound, unsigned int extra_bits) {
unsigned int temp = upper_bound;
unsigned int least_set_bits;
unsigned int most_set_bits;

while( (temp & (temp-1)) != 0) {
temp = temp & (temp-1);
}

if(temp < lower_bound) {
find_bits(lower_bound ^ temp, upper_bound ^ temp, extra_bits + 1);
return;
}

if(lower_bound == 0) {
least_set_bits = 0 + extra_bits;
} else {
least_set_bits = 1 + extra_bits;
}

if(temp - 1 < lower_bound) {
most_set_bits = least_set_bits;
} else {
temp--;
most_set_bits = 0;
while(temp > 0) {
temp >>= 1;
most_set_bits++;
}
most_set_bits += extra_bits;
}

printf("Least set bits is %u, most set bits is %u.\n", least_set_bits, most_set_bits);
}
``````

## bit manipulation:print the next smallest and largest numbers with same no of 1 bits

By : PatrickBlume
Date : March 29 2020, 07:55 AM
will help you Given an integer , print the next smallest and next largest number that have the same number of 1 bits in their binary representation , for next high you can use Hakmem 175 :
ITEM 175 (Gosper):
code :
``````unsigned nexthi_same_count_ones(unsigned a) {
/* works for any word length */
unsigned c = (a & -a);
unsigned r = a+c;
return (((r ^ a) >> 2) / c) | r;
}
``````

## Simple logic problem: Finding largest and smallest number among 3 numbers

By : user3000638
Date : March 29 2020, 07:55 AM
it fixes the issue I am creating a pseudocode in determining the smallest and largest number among 3 numbers: , Let's say you've got arbitrary numbers x, y, z.
Pseudocode:
code :
``````largest = x
smallest = x

if (y > largest)  then largest = y
if (z > largest)  then largest = z

if (y < smallest) then smallest = y
if (z < smallest) then smallest = z
``````
``````array = [x, y, z]
arrays.sort()
largest  = array
smallest = array
``````
``````array = [x, y, z]
largest  = max(array)
smallest = min(array)
``````
``````array = [x, y, z]
(largest, smallest) = (max(array), min(array))
``````
``````array.insert([x, y, z])
smallest = array
largest = array
``````

## Java Program: finding smallest and largest number of 3 numbers in an array

By : user3732031
Date : March 29 2020, 07:55 AM
will be helpful for those in need Smallest is always initialized as 0 here: int smallest = numbers. Unless the user enters a value smaller than 0, smallest value will stay 0. Use Integer.MAX_VALUE (int smallest = Integer.MAX_VALUE) instead to ensure that the smallest number will actually be selected.

## Finding the smallest AND largest number in a list of numbers inputted by the user

By : Alexandre
Date : March 29 2020, 07:55 AM
hope this fix your issue i needed help displaying the average, smallest and highest number inputted by the user, but i can only display the average and largest numbers. there is another question like this on here but it wasn't exactly solved, giving the smallest number only. if there is a different way other than Math.min and Math.max, that would be greatly appreciated. , Can just add a simple if/else if statement
code :
``````import java.text.DecimalFormat;
import java.util.Scanner;

public class ProblemSet3_1 {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
DecimalFormat dec = new DecimalFormat();

int snum = 0;
int height = 0;
double avg = 0;
int largest = 0;
int smallest = 0;

System.out.print("How much students are in your class?" + '\n');
snum = input.nextInt();

for (int i = 1; i <= snum; i++) {
System.out.print("How tall is student #" + i + "?" + '\n');
height = input.nextInt();

if(i == 1) {
smallest = height;
}
else if(height < smallest){
smallest = height;
}
if (height > largest) {
largest = height;
}

}
System.out.print("The average height is " + avg + ", while the tallest is " + largest + " , and the shortest is " + smallest +  ".");

}
}
``````

## Finding the smallest number and largest number from a list of random numbers generated

By : Ramachandrareddy Kon
Date : March 29 2020, 07:55 AM
wish helps you The problem is with the initial values that you picked for LARGE and SMALL: you set both of them to 0, which is incorrect: you should set them both to the first random number that you generate.
Alternatively, you can set SMALL to the largest possible int, and LARGE to the smallest possible int. Use header and std::numeric_limits class. 