-
September 16th, 2017, 04:31 AM
#1
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 ..
-
September 16th, 2017, 05:31 AM
#2
Re: Question
do you mean, using only int/binary arithmetics ? take a look at http://graphics.stanford.edu/~seander/bithacks.html
-
September 16th, 2017, 06:15 AM
#3
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)
-
September 16th, 2017, 01:21 PM
#4
Re: Question
Originally Posted by aiwa
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.
-
September 17th, 2017, 03:37 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|