Use of the "bit" command?

How does the "bit" command set or clear a bit in a 328P MCU on an Arduino board? I encounter commands such as:

TIMSK1 = bit (TOEI1); //Interrupt on Timer 1 overflow

This seems to cause a read of the TOEI1 bit in a register and then set TIMSK1 to that bit value, 1 or 0. So I don't understand how the bit command works with MCU register names and bit names. I'd appreciate help. Thank you.

Jon

p.s. Whoever came up with the name "bit" for a command didn't consider the problem of trying to find examples by searching for "bit" on the web. Even in combination with other words it's not helpful.

jontitus:
p.s. Whoever came up with the name "bit" for a command didn't consider the problem of trying to find examples by searching for "bit" on the web. Even in combination with other words it's not helpful.

Strange, I googled "arduino bit", and the first result was bit() - Arduino Reference.

Indeed, I found that section of the Arduino Reference, too. But the reference material suffers from lack of examples. My original message included this line of code:

TIMSK1 = bit (TOEI1); //Interrupt on Timer 1 overflow

It seems to say, set the contents of the TIMSK1 to the value found in the TOEI1 bit. That doesn't make sense to me. The code's author seems to use the statement above to SET the TOEI1 bit to a logic-1, but I cannot find anything that explains this operation. Also, how would code differ if I want to set this bit to a zero? Again, lack of examples in the reference leaves many beginners scratching their head.

I'd just like a clear explanation. Thanks.

jontitus:
But the reference material suffers from lack of examples.

How about if this was added to the documentation?:

void setup() {
  Serial.begin(9600);
  while (!Serial) {}  // wait for serial port to connect. Needed for native USB port only

  Serial.println(bit(0), BIN); // value in binary is 1
  Serial.println(bit(0)); // value in decimal is 1

  Serial.println(bit(1), BIN); // value in binary is 10
  Serial.println(bit(1)); // value in decimal is 2

  Serial.println(bit(2), BIN); // value in binary is 100
  Serial.println(bit(2)); // value in decimal is 4

  Serial.println(bit(3), BIN); // value in binary is 1000
  Serial.println(bit(3)); // value in decimal is 8
}

void loop() {}

Would that have made it clear? I welcome any suggestions for improvement, or an alternative example I can add to that reference page.

jontitus:
It seems to say, set the contents of the TIMSK1 to the value found in the TOEI1 bit.

If you wanted to get the value found in the TOEI1 bit of TIMSK1, you could use bitRead():

bool TOEI1Value = bitRead(TIMSK1, TOEI1);

jontitus:
The code's author seems to use the statement above to SET the TOEI1 bit to a logic-1

The code will set the TOEI1 bit of TIMSK1 to 1 and all the other bits to 0.

jontitus:
how would code differ if I want to set this bit to a zero?

You could use bitWrite():

bitWrite(TIMSK1, TOEI1, 0);

I encounter commands such as:
TIMSK1 = bit (TOEI1); //Interrupt on Timer 1 overflow

I hope not, because bit 0 of the TimerCounter1 Interrupt Mask Register (TIMSK1) is named TOIE1. Or rather, TOIE1 is defined to mean bit 0 of the TIMSK1 register. TOIE1 is the TimerCounter1 Overflow Interrupt Enable bit.

There is a file called Arduino.h which defines much of the special Arduino environment. It is included by the ide with your sketch.

That file contains

#define bit(b) (1UL << (b))

I believe in the AVR world the individual settings in a register are defined in terms of their bit position, with values for a particular bit ranging from 0 through 7. TOEI1 is one of these. But to write a value to the register, you have to convert the bit position to the value of that bit position, which would be 1,2,4,8,16,32,64 or 128. The "bit" function just makes that conversion. So TOEI1 is defined in the .h file, or somewhere, as zero, but its value in the register is one.

This method is not universal for all processors. Some define everything as the bit value to begin with.