Jump to content

  • Log In with Google      Sign In   
  • Create Account

A stupid thought.

  • You cannot reply to this topic
4 replies to this topic

#1 slicer4ever   Crossbones+   -  Reputation: 3893

Like
2Likes
Like

Posted 11 May 2014 - 11:26 PM

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.

Edited by slicer4ever, 11 May 2014 - 11:51 PM.

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

Sponsor:

#2 frob   Moderators   -  Reputation: 21430

Like
7Likes
Like

Posted 11 May 2014 - 11:53 PM

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!
Check out my personal indie blog at bryanwagstaff.com.

#3 slicer4ever   Crossbones+   -  Reputation: 3893

Like
6Likes
Like

Posted 12 May 2014 - 06:03 AM

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.


Edited by slicer4ever, 12 May 2014 - 01:10 PM.

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

#4 frob   Moderators   -  Reputation: 21430

Like
0Likes
Like

Posted 12 May 2014 - 12:58 PM

Beautiful nightmare fuel. I salute you.
Check out my personal indie blog at bryanwagstaff.com.

#5 wodinoneeye   Members   -  Reputation: 834

Like
0Likes
Like

Posted 14 May 2014 - 07:22 PM

'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*/ }


Edited by wodinoneeye, 14 May 2014 - 07:37 PM.

--------------------------------------------Ratings are Opinion, not Fact





PARTNERS