簡體   English   中英

C ++函數參數欄

[英]C++ Bars in function parameters

int fd = open(JOYSTICK_NAME, O_RDONLY | O_NONBLOCK);

O_RDONLYO_NONBLOCK之間的O_RDONLY是什么意思? 我在OpenGL / GLUT編程中遇到了這個問題,並對語義感到好奇。

這是按位OR運算符 它接受O_RDONLY的各個位,並將它們與O_NONBLOCK的位合並,然后返回合並后的值。

例如,假設O_RDONLY的二進制值為11001100, O_NONBLOCK的二進制值為00001111。將這些邏輯或運算得到11001111。

它是按位或運算符。 它用於累積位標志。

它是兩個操作數的按位或 在這種情況下,操作數都在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操作,並返回結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM