Number Conversion

Hello Everybody!!!

Please let me know how can I convert an integer to another integer variable such that 0 will be 0 & numbers above 0 will be converted to 1.

0-0 1-1 2-1 3-1 4-1 . . .

I want to implement it in If loop such that if dateColumn is 0 then condition is 0+3 otherwise it will be 1+3 i.e.4

if (currentColumn == (dateColumn+3)) currentColumn = 0;

Thanks..

x = !!x

I don't find your question and example very clear.

Your first description implies that you want to test an integer and if its value is 0, then you want to keep it at zero. If, however, that integer has any other positive value, you want to set it to 1.

Your example later seems to imply you might set the integer value to 4 under some condition.

How are you intending to represent in C++ the columns in your example ? By 2 integer arrays ?

Thanks AWOL, I will try it.

6v6gt, actually in my project I had to have 2 variables. One will be isDateColumn & another is dateColumn. If isDateColumn is true then only dateColumn will be considered.

Instead of using 2 variables I am trying to use a single integer (dateColumn). If it is 0 then dateColumn is not present otherwise it will hold the value of date column. For any other value, I have to test currentColumn for 1+3

//if dateColumn is 0, I want to test currentColumn for 3
//If dateColumn is other than 0, I want to test currentColumn for 4

if (currentColumn == (dateColumn+3)){
    currentColumn = 0;
}

AWOL: x = !!x

As long as x>=0

maybe something like this ?

if ( dateColumn == 0 && currentColumn == 3 ) {
  // handle first case 
}
else if ( dateColumn != 0 && currentColumn == 4 ) {
  // handle second case 
}

aarg: As long as x>=0

The original spec said

numbers above 0 will be converted to 1.

, it didn't mention negative numbers.

However, negative numbers will also be converted to one.

6v6gt, this works but I was trying to simplify coding in a single statement. Thanks.

AWOL x = !!x works!!! :) Thank you very much!!!

Can you please explain how it gets 1 from any number greater than 0?

vbdev: Can you please explain how it gets 1 from any number greater than 0?

Any non-zero number. If you look up the "!" operator you can figure it out. But I'm sure he will be happy to explain. By the way, if you feel inclined, you can also say "not not x", it's valid syntax. :)

If you want the equivalent of “sign(x)” which returns 1, 0 or -1 depending on whether the given value is positive, zero or negative, there’s this

int sign (x)
{
 return (x > 0) - (x < 0);
}

In C, any expression that evaluates to non-zero is deemed to have the value true, represented by a one, and any zero expression is false, represented by the value zero.

! is the logical NOT operation - it logically negates the value of the expression which follows it.
So take, for example, an expression that evaluates to 3.
3 is true, so !3 is false, or zero.
Apply the logical NOT to this expression and you have !0 is true, or 1.

vbdev: 6v6gt, this works but I was trying to simplify coding in a single statement. Thanks.

AWOL x = !!x works!!! :) Thank you very much!!!

Can you please explain how it gets 1 from any number greater than 0?

I can't quite see how the two alternatives you have been given both achieve acceptable results for you, but if you are wanting a one-liner to replace the logic I supplied, it is this which returns a boolean result:

( ( dateColumn == 0 && currentColumn == 3 ) || ( dateColumn != 0 && currentColumn == 4 )  )

6v6gt Sir, I think my problem can be solved by both the methods. But I only tried the !!x method & it works as required.

Sorry, I had posted only a small portion of my code. Actually I am incrementing the value of currentColumn on each press of TAB key (PS2 keyboard) in a loop. Normally it will increment upto 2 (0-1-2-0-1-2-0..) If dateColumn is also present it will increment upto 3 (0-1-2-3-0..)

My code becomes as under:

else if (c == PS2_TAB) {      //change the column number from 0-4 & back to 0
      Serial.println("TAB");
      if (currentColumn == (!!dateColumn+2)) currentColumn = 0; // !!dateColumn returns 0 for 0 & 1 for any number more than 0
      else currentColumn++;
}

If dateColumn is present the currentColumn will loop through 0-1-2-3-0-1-2-3-0....at each press of TAB key. Otherwise it will be like 0-1-2-0-1-2-0...

The same variable dateColumn can be used to get the column used for showing date. Thanks everybody & specially AWOL for his nice explanation. BTW, the sign(x) function is very interesting :)