I'm learning about Unix serial programming and I have noticed two uses of bitwise operators as follows :
tty.c_cflag &= ~CRTSCTS; // Disable RTS/CTS hardware flow control
tty.c_cflag |= CRTSCTS; // Enable RTS/CTS hardware flow control
any appearance of &= and ~ means disabling and |= means enabling a feature. Is this some universal convention for enabling/disabling features when dealing with flags and bitwise operations? in other words, are those uses of bitwise operations used specifically for this purpose? or there's a mask behind this that Unix uses and it's different with every other library? e.g. I can use |= for disabling instead of enabling features.
After setting/clearing the bit array, how does the library check which features were set and call events accordingly? does it use if-statements for each bit? I'm trying to build something similar and I wonder if using if-statements is efficient after the user passes the features he wants?
I'm learning about Unix serial programming
To be clear, you seem to be referring to the POSIX interface for serial terminals, aka termios. Note that this API combines the configuration of serial ports with serial terminals.
Also note that the bit values for termios attributes depend on the specific OS and CPU architecture.
I have noticed two uses of bitwise operators ...
Which are formally known as Boolean arithmetic.
Refer to Setting Terminal Modes Properly for the reasoning for using bitwise operations with termios attributes.
When you set terminal modes, you should call tcgetattr() first to get the current modes of the particular terminal device, modify only those modes that you are really interested in, and store the result with tcsetattr().
... you should start with the current value of the member and alter only the flags whose values matter in your program, leaving any other flags unchanged.
any appearance of &= and ~ means disabling and |= means enabling a feature. Is this some universal convention for enabling/disabling features when dealing with flags and bitwise operations?
Sort of, for software anyway.
Beware that you are conflating Boolean values & arithmetic with features and enabling/disabling them.
For termios (and the typical API), a binary 1
is equated to the condition the feature is enabled
.
However some interfaces, especially hardware interfaces, might use inverted logic, where a 1
means de-asserted or false
.
For example most RS-232 modem control and handshake signals use inverted logic.
in other words, are those uses of bitwise operations used specifically for this purpose?
The "purpose" is to set and/or clear only specific bits in structure members (without modifying other bits/sflags), which will communicate to the OS the terminal and communication settings your program wants to use (when your program passes the termios structure using the tcsetattr() syscall).
In general your program will not know the pre-existing termios configuration, nor should it care. So the best practice (for robustness/portability) is for your program to enable and/or disable all attributes/flags as needed (using bitwise operations).
or there's a mask behind this that Unix uses and it's different with every other library?
"Mask"?
Termios is an API (i.e. application program interface for interacting with an OS subsystem), not a "library" (i.e. a collection of routines linked with your application program).
Are you asking a XY question?
I can use |= for disabling instead of enabling features.
A Boolean OR operation using a 1
bit will always result in a set bit.
So if the set bit means that the feature is enabled (which is typical for the termios API), then the answer is "no, that will not disable a feature".
The answer could be affirmative if inverted logic is being used.