int fd = open(JOYSTICK_NAME, O_RDONLY | O_NONBLOCK);
O_RDONLY
和O_NONBLOCK
之間的酒吧是什麼意思?我在OpenGL/GLUT編程中遇到過這個問題,我很好奇它的語義。C++酒吧功能參數
int fd = open(JOYSTICK_NAME, O_RDONLY | O_NONBLOCK);
O_RDONLY
和O_NONBLOCK
之間的酒吧是什麼意思?我在OpenGL/GLUT編程中遇到過這個問題,我很好奇它的語義。C++酒吧功能參數
這是bitwise OR operator。它採用O_RDONLY
中的各個位並將它們與O_NONBLOCK
中的位組合,並返回組合值。
例如,假設二進制值O_RDONLY
是11001100和O_NONBLOCK
二進制值是00001111.的OR-ing這些結合在一起產生11001111.
這是按位運算符。它用來累積比特位。
這是兩個操作數的bitwise OR。在這種情況下,操作數都在fcntl.h
定義:
/* File access modes for open() and fcntl(). POSIX Table 6-6. */
#define O_RDONLY 0 /* open(name, O_RDONLY) opens read only */
#define O_WRONLY 1 /* open(name, O_WRONLY) opens write only */
#define O_RDWR 2 /* open(name, O_RDWR) opens read/write */
...
/* File status flags for open() and fcntl(). POSIX Table 6-5. */
#define O_APPEND 02000 /* set append mode */
#define O_NONBLOCK 04000 /* no delay */
所以O_RDONLY
:
000 000 000 000 (0)
被進行或運算與O_NONBLOCK
:
100 000 000 000 (04000 in octal notation)
結果因此是:
100 000 000 000 (0400)
不是一個非常令人興奮的例子,但它就是這樣做的...
這是一個按位或。它採用兩個參數(O_RDONLY和O_NONBLOCK)的二進制表示形式,並將OR操作應用於它們,並返回結果。
你可能想看看一些基本的C++文本。在這裏看到一個由C++人認爲是好的列表:http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – 2010-11-29 16:40:19