# 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.

## Recommended Posts

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:

#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:
#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 on other sites
Beautiful nightmare fuel. I salute you.

##### Share on other sites

'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

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5
A4L
11

• 9
• 12
• 16
• 26
• 10
• ### Forum Statistics

• Total Topics
633769
• Total Posts
3013754
×