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 & (temp1)) != 0) {
temp = temp & (temp1);
}
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 & (temp1)) != 0) {
temp = temp & (temp1);
}
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 & (temp1)) != 0) {
temp = temp & (temp1);
}
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);
}
Share :

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[2]
smallest = array[0]
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[0]
largest = array[2]

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[0]. 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

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.



Related Posts :
