Jump to content
  • Advertisement
Sign in to follow this  
slicer4ever

A stupid thought.

This topic is 1679 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement

'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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!