r/badcode • u/rs277_ • Aug 16 '20
c++ Another function to see if something is even or not
2
103
u/sekex Aug 16 '20 edited Aug 16 '20
Could actually be more efficient for small values than doing a modulo on the absolute value considering a division can take up to 80 cycles. (don't quote me on this)
Edit: It's actually a division by 2, so a simple bitshift (<<) would do in 1 cycle
121
u/xVirusHDx Aug 16 '20
aight I’ll quote you in my graduation paper
48
u/StuntHacks Aug 16 '20
As is common knowledge, a division can take up to 80 cycles. 1 This means it can be concluded that the given code would be more efficient under the right circumstances when compared to traditional methods (namely the modulo-operation), and should therefore not be considered to be produced by a master of Italian cuisine.
1 /u/sekex, on reddit.com (accessed 16th of August, 2020)
55
u/damyvv Aug 16 '20
Even better, modulo 2 is the same as just checking the last bit. So even a bitshift is not necessary to check if a number is even. A simple 'and' instruction is all that it takes.
23
9
6
17
u/Timmy_the_tortoise Aug 16 '20
Most compilers would do that optimisation for division by 2 (or powers thereof) automatically as well.
6
4
Aug 16 '20
Even so, would that efficiency really make enough of a difference to justify this?
11
u/sekex Aug 16 '20
Depends on your industry. In HPC/HFT/video games, definitely. Furthermore if you have to run this task billions of time, a 40 times speed gain makes a huge difference (hours vs days.)
2
Aug 16 '20
Fair enough, but doesn’t it also depend on the input here? I mean I guess it’s only O(n), but if the input is 183673827199181...
Edit: nevermind I realized you said for small values, but I would still say that you would really need to justify this and know that the values would always be small.
11
u/sekex Aug 16 '20
Someone in the comments pointed out that you could just write !(a & 1) as you only need to look at the last bit
1
2
10
u/Ma_124 Aug 16 '20 edited Aug 16 '20
Worst thing is that the compiler doesn't even optimize it: https://godbolt.org/z/G7r3dT
5
u/redditsoaddicting Aug 16 '20
You didn't even use any optimization flags. However, Clang seems to optimize it a bit more with flags.
5
u/Ma_124 Aug 16 '20
I'm sorry. I'm not very experienced with gcc and thought optimization was enabled by default but even with
-O3it doesn't seem to optimize the loop away. But if you still think that I missed a flag feel free to comment and I'll amend my comment.4
u/Mr2-1782Man Aug 17 '20
Compiler optimizations and why or why they don't happen aren't immediately obvious. My suspicion is that gcc only optimizes away a loop if the number of iterations is known at compile time. In my experience clang does a better job of working out the math and figuring it out.
1
u/AugustinCauchy Aug 16 '20
Try moving the return to the end of the function, clang goes completely ballistic.
2
u/redditsoaddicting Aug 16 '20
Nice, it looks a little bit better with
-mno-ssebut it's still pretty bad lol2
u/Mr2-1782Man Aug 17 '20
I think that's because clang isn't doing a def/use check on input so its treating them as separate loops and unrolling them.
18
u/cthulhu_is_right Aug 16 '20
Gotta admit, a lot of my code prob belongs here
4
u/OptimalMain Aug 16 '20
Probably my hacks too.. But at least I knew I could do modulo 2.
Learned a new trick with just checking the least significant bit though!
25
u/Astrokiwi Aug 16 '20
Would something like !(input && 0x00000001) work?
33
u/Timmy_the_tortoise Aug 16 '20 edited Aug 16 '20
In C++ && is a Boolean operator, what you’re looking for is & which would perform a bit wise AND, which would indeed work. Not sure why you got downvoted for not much more than a typo... perfectly decent solution. Possibly the most optimal, though a good compiler would probably optimise % 2 to the same operation anyway.
8
u/Astrokiwi Aug 16 '20
I think people are assuming I've been whooshed and am actually trying to correct OP.
And yeah, you're right,
0x00000001evaluates to true, so with my mistake!(input && 0x00000001)reduces to!input.
5
5
u/Dr_Insano_MD Aug 16 '20
2
u/techgineer13 Aug 16 '20
1
u/sneakpeekbot Aug 16 '20
Here's a sneak peek of /r/SubsIFellFor using the top posts of all time!
#1: hes so cute though 😍😍❤️ | 185 comments
#2: I'm in love | 129 comments
#3: Are meta posts allowed? | 126 commentsI'm a bot, beep boop | Downvote to remove | Contact ^^me | ^^Info | ^^Opt-out
1
1
3
u/DarthPiotr Aug 16 '20
I think it's high time when someone should make an API for this. Why do we have to code it ourselves? /s
15
5
1
u/das_Keks Aug 16 '20 edited Aug 16 '20
Negative part should use i-- As it is, it's an infinite loop for negative numbers.
EDIT: I'm an idiot, if it's negative of course we have to count "up".
3
u/rs277_ Aug 16 '20
It doesn't, I made the loop count up from the input number towards 0, It's just written differently than most loops, but I found this more convenient
2
2
2
2
2
2
2
u/Neat-Flatworm7025 Oct 29 '20
The first way I checked if a integer was even or odd was to convert to float, the divide by two, then convert to string, then loop through the string to check for a decimal place.
2
1
u/Abangranga Aug 16 '20
Ruby allows for concise solutions to the even number problem oppressing the progress of humanity. For example:
def highly_technical_is_even?(input, counter = 0)
if input.zero?
return (counter % 2).zero?
else
highly_technical_is_even?(input - 1, counter + 1)
end
end
def equality_is_even?(input)
((input / 2) * 2) == input
end1
1
1
0
0
-2
-2
-3
u/myevillaugh C# Aug 16 '20
It should be an else if. And both returns should be replaced with a return statement outside of the if block. http://imgur.com/gallery/euRu9De
4
0
0
Aug 16 '20
[deleted]
19
u/Lysdal Aug 16 '20
damn you're so smart how did you come to with that, such a shame the op didn't realize it was that simple!
1
1
96
u/Bliztle Aug 16 '20
Does this even compile? Seeing as the program probably doesn't know the if statements account for all values, wouldn't this say "not all code paths return a value"?