Skip to main content
GameDev.net gamedev.net
🔒 Locked

A stupid thought.

Started by slicer4ever May 12, 2014 at 5:26 AM 3 replies 3.5k views
Original Post
slicer4ever
slicer4ever
so i'm lying in bed and had the stupidest thought about code i've ever had, that i just had to share this one:

what if you want to check if a value is equal to 2 numbers that are off by only 1 number, i.e: 10 and 11. you take the number your checking, and or it with 1, then check if it's equal to the odd number of the 2 values.

so thusly i give you:

http://codepad.org/Xpa8z9yr
#include<stdio.h>

int main(int argc, char **argv){
    int oddnbr = 11;
    for(int i=0;i<100;i++) if((i|1)==oddnbr) printf("i: %d\n", i);
    return 0;
}
and just incase your even number is greater than your odd number:
http://codepad.org/mRB6PBvL
#include<stdio.h>

int main(int argc, char **argv){
    int oddnbr = 11;
    for(int i=0;i<100;i++) if(((i-1)|1)==oddnbr) printf("i: %d\n", i);
    return 0;
}
alright, ganna head back to bed now.
frob
frob
Yeah, probably not the most optimizer-friendly version of that code.

But I suppose it works, assuming you take care about negative numbers, that you "add" your number in the correct direction for the bit pattern.

Personally I would prefer the more straightforward if( i == a || i == b ). More friendly to those who must maintain it, and to the optimizer.


Great coding horror!
slicer4ever
slicer4ever

but frob, i can already forsee codebases doing the following:



bool Is(unsigned int value, unsigned int a, unsigned int b){
   unsigned int g = a>b?a:b;
   unsigned int s = a>b?b:a;
   if(g-s==1){
    if(g&1) return (value|1)==g;
    else return ((value-1)|1)==s;
   }else return value == a || value == b;
}

but no, seriously don't think i'm actually advocating this use, i think if anyone actually uses it in real life there is a special place in hell for them.

frob
frob
Beautiful nightmare fuel. I salute you.
wodinoneeye
wodinoneeye

'off by one' is vague - you mean +1 (matching n, n+1) (I interpret 'off by one' as +/- 1)

lets try some example value = 2 and value = 3

BTW -you should express your code as supplying "The Value" input and show what you do to get the 'oddnbr' used in the test, as THAT IS should be part of your algorthm's generalized method...)

candidates ORed with 0b00000001 (1) supposedly being equal to some calculated 'oddnbr' (however THAT is calculated)

case value = 2 oddnbr = 3 ...

2 0b00000010 | 0b00000001 == 0b00000011 compared to 0b00000011 works

3 0b00000011 | 0b00000001 == 0b00000011 compared to 0b00000011 works

OK

case value = 3 oddnbr = 3 ...?? or 4 or ???? how to get this ??????????

3 0b00000011 | 0b00000001 == 0b00000011 compared to 0b00000011 works

4 0b00000100 | 0b00000001 == 0b00000101 compared to 0b00000011 FAILS is equal to 5 (YET is 'off by one' from 3)

so this system doesnt work at all because there are cases (illustrated) where there isnt a single 'oddnbr' for it to work with certain values of 'value'.

(note -- this is ignoring further complications if negative numbers are being involved )

Isnt it really quite simpler (and alot clearer) just to do a double clause test

(second claus is usually shortcircuited (skipped when first clause TRUE) by even mildly apt compilers)

if ((candidate == value) || (candidate == value+1)) { /* it is good so do something*/ }

--------------------------------------------[size="1"]Ratings are Opinion, not Fact

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.