2

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.

Albert Lazaro de Lara
  • 2,540
  • 6
  • 26
  • 41
  • 1
    Possible duplicate of https://stackoverflow.com/questions/610916/easiest-way-to-flip-a-boolean-value – Attersson May 12 '18 at 17:04
  • You question is not clear. What do you mean by "flip"? Perhaps negate? C already treats integers as books and. In fact, there is no special Boolean type in C. A zero is interpreted as false, any other value - as true. – DYZ May 12 '18 at 17:05
  • 1
    Anyway you can actually do `val = !val` .... – Attersson May 12 '18 at 17:05
  • 1
    In C zero is falsy and everything else is truthy. It looks like you want one truthy and everything else falsy. In that case, you can simplify to `val = val != 1;`. Otherwise, `val = !val` works just fine. – Quentin May 12 '18 at 17:06
  • 1
    @DyZ [hello, time traveler. Have a nice stay](http://en.cppreference.com/w/c/types/boolean) :) – Quentin May 12 '18 at 17:07
  • Using just integer calculations, you can do `i = (i-1) * -1` every time you want to flip. A bit verbose though. – ENBYSS May 12 '18 at 18:15

3 Answers3

4

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.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
1

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

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
-1

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
Cpp plus 1
  • 990
  • 8
  • 25
  • 2
    Non-portable - All bitwise operations on signed types are implementation-defined. Plus it is much less clear than Summit's answer. – something_clever May 12 '18 at 17:22