logo
Tags down

shadow

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


Share : facebook icon twitter icon

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
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;
        int add = 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();

            add += height;
            avg = add / i;
            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.
Related Posts Related Posts :
  • Why do these two execvp produce different results?
  • Pass uintmax_t or size_t to custom printf conversion specifier
  • Why does free() leaves stuff in memory?
  • Why pointers can't be used to index arrays?
  • memory allocation eror in C
  • C custom datatypes mapped to C datatypes grouped under a single struct
  • pipe() data is not transferred to child process
  • Getting a core dump from a simple C program
  • Fatal error on makefile, need to understand the problem
  • How can I add a delay of 90 minutes when a port has gone from 0 to 1?
  • To use strcpy or not
  • the usage of strtok() in c shows warnings and returns segmentation fault(core dumped)
  • Trouble allocating array of structs
  • Only first char of user input used in array
  • Why does "int x = 5; printf("%d %d %d", x==5, x=10, x==5);" in C print "0 10 0"?
  • How to scan specific string format in C?
  • sscanf skipping the final value when reading multiple values from a line
  • How can I access full memory space in FreeDOS with C application
  • Semantics of sem_getvalue() in POSIX
  • What does the [x,y] symbol mean in a multidimensional array access?
  • compilation error: cast from pointer to integer of different size
  • why am i getting compilation error "error: conflicting types for ‘ptr’ " for the following code? static int va
  • Why does getw return -1 when trying to read a character?
  • Why C program in whch two functions call each other recursively gives segmentation fault on linux?
  • ssize_t is undefined
  • Passing a generic argument in a C function
  • Restoring stdout after redirecting it via freopen()
  • Tuples "cannot be marshalled in a foreign call" when trying to export a Haskell function to C
  • Dereferencing double pointer to pass to recursive function
  • Why is the output different in these two scenarios
  • what will happen if we dont use free() for allocated memory
  • counting words from user input
  • Why am I not getting the concatenated string?
  • not understandable pointers errors
  • Issue with function memory allocation
  • /usr/bin/ld: cannot find -lioutil in Makefile
  • Segmentation Fault when finding longest word in input
  • undefined reference to `palloc'
  • DMA transfer taking more time than CPU transfer
  • How can I make my code find solution while working at specific coordination?
  • How to fix implicit declaration of function?
  • Writing a file line by line in C
  • "accept" always returns 1, but no descriptor has been closed
  • How to fix 'Bad file descriptor' after implementing here-string redirection in my own shell
  • Why this output string to number?
  • Is there any way to make this adding an infinite?
  • for loop is iterating printf written after scanf only once
  • How to efficienty count from 0000 to 9999 in a digit display?
  • Implicit conversion and explicit conversion in c
  • Attempting to convert a value into 2s complement in C
  • else statement is resulting in identifier expected in c 13
  • Thread-safety vs atomicity in C
  • Segmentation fault - Trying to read binary file into memory
  • How to make a variable declared in constructor visible in the main file
  • What is the fastest way to reverse a power of two in C?
  • Reading and writing structure to binary file in C, then print structure elements
  • Why casting double to int might give different results?
  • Greedy algorithm in C not returning any values?
  • How to find base address of same char in string array using pointers in C language?
  • Quick question, why is scanf_s throwing an exception in runtime here? I am very confused
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk