CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Question

  1. #1
    Join Date
    Sep 2017
    Posts
    2

    Question

    Hi guys , i have a question . Example : i declared 2 int variables .. How do i find which number is minimum and which maximum without using cicles ( for , dowhile , while , if , repeat etc ) ? It is possible ? I asked many people but i didn't find a answer ..

  2. #2
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Question

    do you mean, using only int/binary arithmetics ? take a look at http://graphics.stanford.edu/~seander/bithacks.html

  3. #3
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Question

    How do i find which number is minimum and which maximum without using cicles
    Why - what are trying to achieve? If using c++ then the STL has min() and max(). For min() see http://www.cplusplus.com/reference/algorithm/min/
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Feb 2017
    Posts
    677

    Re: Question

    Quote Originally Posted by aiwa View Post
    How do i find which number is minimum and which maximum without using cicles ( for , dowhile , while , if , repeat etc ) ? It is possible ?
    You can use the ternary operator like this,
    Code:
    int max(int a, int b) {
        return (a>b) ? a : b; 
    }
    It doesn't use any of the control structures you mention but it still involves a conditional selection so it's not branch free (which I suppose your question is about?).

    Still there are true branch free alternatives indeed as superbonzo's link shows. Here's another one,

    http://bits.stephan-brumme.com/minmax.html

    If you not only want to know whether it is possible to have min/max without branching but want to actually use it to optimize real code I think the best option is to use std::min/std::max as 2kaud suggests. I'm sure the C++ standard library implementations of most quality compilers will produce branch free code for these functions in speed optimization mode. In fact I'm so sure I'll eat my hat if they don't.
    Last edited by wolle; September 17th, 2017 at 12:42 AM.

  5. #5
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Question

    moreover, the min/max standard family of functions also works with references, enabling things like

    Code:
    auto bounds = std::minmax(a,b); 
    
    bounds.first--; // say, decrement the minimum
    bounds.second++; // increment the maximum
    }
    this may lead to cleaner/faster code in some circumstances ...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured