Reputation: 2710
I'm looking a way to flip an integer value (8bits) using it as a boolean (0 -> False, 1 -> True).
On many languages you can do val = !val
to change the value. The only thing I have done is val = (val == 1) ? 0 : 1
. I don't know if in C can work with a bite value.
It is for an old hardware with a 8bit processor, so the idea of use boolean
is not possible, and I can't install external libraries.
Upvotes: 2
Views: 944
Reputation: 47923
I posted one answer, but I may have misread the question. If you have an integer variable -- it might be int
, short int
, or char
-- and you want to have it cycle back and forth 0, 1, 0, 1 (which you can interpret as false, true, false, true), there are two about equally good ways to do it. You could do:
i = !a;
This first way emphasize the "Boolean" nature of the variable. Or, you could do:
i = 1 - i;
This second way is purely numeric.
But either way will work perfectly well. In either case, i
is guaranteed to alternate 0, 1, 0, 1, ...
You could also use
i = i ? 0 : 1;
or
i = (i == 0) ? 1 : 0;
Both of these will work, too, but they're basically equivalent to i = !i
.
In your question you suggested
i = (i == 1) ? 0 : 1;
This would mostly work, but it looks weird to my eye. Also it would do the wrong thing if i
ever ended up containing 2 (or any value other than 0 or 1).
Upvotes: 1
Reputation: 1010
Adding to Summit's answer, you can also do this:
#define TRUE ~0U
#define FALSE 0U
#ifndef BOOL
#define BOOL unsigned int;
#endif
int main() {
BOOL var = TRUE;
var = ~var; //Negate var and set it to false.
var = ~var; //Negate var again and set it to true.
return EXIT_SUCCESS;
}
If you are using at least C99, then you can change
#ifndef BOOL
#define BOOL unsigned int;
#endif
to:
#include <stdint.h>
#ifndef BOOL
#define BOOL uint_fast8_t;
#endif
Upvotes: -1
Reputation: 47923
In C a value of 0 is considered "false", and any other (nonzero) value is considered "true". So one, very explicit way to convert an arbitrary int
value to an explicitly true-or-false Boolean value is to use the (C99 and later) bool
type:
#include <stdbool.h>
int i;
bool b;
b = (i != 0) ? true : false;
But since the !=
operator essentially "returns" a Boolean value, you can also do
b = (i != 0);
And since the zero/nonzero definition is built in to the language, you can also just do
b = i;
But you don't have to use type bool
. If you have an arbitrary-value int and you want to force it to 0/1 "in place", you have the same sorts of options:
i = (i != 0) ? 1 : 0;
Or
i = (i != 0);
Or there's another common trick:
i = !!i;
This last is concise, popular, and somewhat obscure. It changes zero/nonzero to 1/0 and then back to 0/1.
I've shown these examples using type int
, but they would all work just as well with short int
or char
(i.e. byte).
One more thing. In your question you wrote val = (val == 1) ? 0 : 1
, but that's basically meaningless. In C, at least, everything (everything!) follows from whether a value is zero or nonzero. Explicit comparisons with 1 are almost always a bad idea, if not a downright error.
Upvotes: 4