Overflow in bit calculation I'm having a double buffer to write data from sensors to SD card. inBuf points to the buffer that are written into (from sensors). The other buffer write data to SD card.

char buffer[2][BUFLEN];
volatile int inBuf = 0;

inBuf = 1 - inBuf;


However, the update of inBuf does not work. Firstly inBuf is 0, and after that inBuf becomes 32!!! I tried different data types: boolean, uint8_t, int and the results are the same.

How could I fix this problem? Also, what datatype should I use for inBuf, as it's really just one bit?

I ended up using the following:

inBuf = inBuf? 0:1; 


but seems really complicated and unnecessary.

EDITS:
Sorry for was not being clear previously. It turns out that I had an overflow in the code:

char buffer[2][BUFLEN];
volatile uint16_t bufferCounter;
volatile int inBuf = 0;
volatile bool outBufHasData = false;


Inside the interrupt handler:

if (bufferCounter &lt; BUFLEN - SAMPLESIZEINCHAR + 1) {
  epoch = now();
  proximity = vcnl.readProximity();
  ambientLight = vcnl.readAmbient();

  // format to be fixed length as opposed to variable length
  // sacrifice overhead in written data with simpler and more robust code
  // since we know exactly how much to increment after each write
  int written = snprintf(&amp;buffer[inBuf][bufferCounter], SAMPLESIZEINCHAR,
        "%10lu,%5d,%5d\n",
        epoch, proximity, ambientLight);

  if (written &gt; 0) {
    bufferCounter += written;
  }
} else {
  // only switch buffer if out buffer is written to SD card
  if (!outBufHasData) {
    inBuf = inBuf? 0:1;    
    bufferCounter = 0;
    outBufHasData = true;
  }
}


Then in the for loop, where the buffer is written to SD card when the buffer is full:

  if (outBufHasData) {
    myFile.print(buffer[1 - inBuf]);
    myFile.close();
    outBufHasData = false;
  }


Previously, I had bufferCounter &lt; BUFLEN !! Thus I was writing over the memory of the buffer, and spill into bufferCounter and inBuf. That's why I got the value 32 of inBuf, which is the ASCII code of space. Mystery resolved!!
If you want a variable to be 'yes' and 'no' or perhaps '0' and '1', then you can use a boolean which is 'true' or 'false'. With a boolean you can use logic operators:

boolean dataReady = false;
dataReady = !dataReady;
if( dataReady) ...


A boolean on a 8-bit AVR chip (Arduino Uno,Leonardo,Nano,Mega) is 8 bit. You could also use a byte and make it 0 or 1. 

A 8-bit variable is often used when a interrupt routine sets a flag. By making that flag only 8 bit and volatile, it can be used in the loop function as well without the need to disable the interrupts. Reading and writing a 8 bit variable can not be interrupted by an interrupt routine.

Using 8 bits when only 1 bit is needed is normal. You don't have to worry about that.

When you need hundreds of 1 bit variables, then it is possible to select each bit with macro's or a lot #define to select each bit. This is almost never used in normal code, but almost always used for the registers of a sensor.
And last but not least: a struct with bit fields packs the bits. That can be handy sometimes.