Problem with BITREAD

Hello,
I have a problem with bitRead.
If I run bitRead passing in the serial monitor the letter 'A', so that the function becomes bitRead ('A', 0), rightly I get a value of 1, because the ASCII value of 'A' is 1000001 and the first position is 1.
Everything is ok.
The problems arise when I try to run bitRead after all other functions.
In my code, bitRead always returns zero and I do not understand why.
It seems that the variable 'Valbit' is in some way affected by the above functions, and the end result is always 0.
The problem does not occur when I run bitRead alone without the other functions above.
Why is it so?
Thank you all for the help.

char Valbit;
void setup()
{
Serial.begin(9600);

}
void loop()
{
if (Serial.available()>0){

Valbit=Serial.read();

Serial.print("VALUE= ");
Serial.print(Valbit);

Serial.print(" DECIMAL VAL= ");
Serial.print(Valbit,DEC);

Serial.print(" BINARY VAL= ");
Serial.println(Valbit,BIN);

//bitSet(Valbit, 1); Sets (writes a 1 to) the given bitPositionof variable x
byte bitset=bitSet(Valbit, 0);

Serial.print("bitSet= ");
Serial.write(bitset);

Serial.print(" DECIMAL bitSet= ");
Serial.print(bitset,DEC);

Serial.print(" BINARY bitSet= ");
Serial.println(bitset,BIN);

//bitClear(x, bitPosition); Clears (writes a 0 to) the given bitPosition of variable x
byte bitclear=bitClear(Valbit, 0);

Serial.print("bitClear= ");
Serial.write(bitclear);

Serial.print(" DECIMAL bitClear= ");
Serial.print(bitclear,DEC);

Serial.print(" BINARY bitClear= ");
Serial.println(bitclear,BIN);

//bitWrite(x, bitPosition, value) Sets the given value (as 0 or 1) of the bit at the given bitPositionof variable x

byte bitwrite=bitWrite(Valbit, 4,0);

Serial.print("bitWrite= ");
Serial.write(bitwrite);

Serial.print(" DECIMAL bitwrite= ");
Serial.print(bitwrite,DEC);

Serial.print(" BINARY bitwrite= ");
Serial.println(bitwrite,BIN);

//bitRead(x, bitPosition); Returns the value (as 0 or 1) of the bit at the given bitPositionof variable x

int bitread=bitRead(Valbit, 0);

Serial.print("bitRead= ");
Serial.println(bitread);
}

}

The problems arise when I try to run bitRead after all other functions.
In my code, bitRead always returns zero and I do not understand why.

Ignoring all the Serial.print() statements, you have:

    Valbit=Serial.read();
    byte bitset=bitSet(Valbit, 0);
    byte bitclear=bitClear(Valbit, 0);
    byte bitwrite=bitWrite(Valbit, 4,0);
    int bitread=bitRead(Valbit, 0);

After setting bit 0 to 1, you clear bit 0 (set it to 0). Then, you write a 0 to bit 4, then you read the bit you cleared. No wonder you always get 0.

Thank you all!
I realized the mistake! :slight_smile: XD
Stupidly I thought that the value would be altered only in the other variables and that “Valbit” would have always remained clean. :roll_eyes:
I had not considered that “Valbit” is modified by the functions, and then is assigned to other variables. :fearful:
I had imagined that only the other variables contain the value of Valbit changed, while the original value of Valbit remain intact.
Hehehe … that stupid mistake. :wink:
Thank you again!