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

What does the bar between O_RDONLY and O_NONBLOCK mean? I've encountered this in OpenGL/GLUT programming and I was curious on the semantics.

  • You might want to check out some basic C++ texts. See here for a list of ones considered to be good by the C++ people on SO: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – John Dibling Nov 29 '10 at 16:40
  • Thanks! Will utilize this. How come there are only two books in above intermediate? –  Nov 29 '10 at 16:42
  • are you for real? I see that you answered some C++ questions, yet you don't know the basic operators? Python, with which you seem more familiar, has the same operator, did it ring the bell? Or is it some kind of a joke? – davka Nov 29 '10 at 17:05
  • I wouldn't consider bitwise operations basic. You honestly expect a student to encounter every semantic nuance in a language? That's why there are questions to be asked. Why didn't I google? Googling 'bar in between C++ functions' doesn't really cut it. –  Nov 30 '10 at 02:16

5 Answers5

2

It's the bitwise-or operator. It's used to accumulate bitflags.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
2

It is a bitwise OR of the two operands. In this case the operands are both defined in 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 */

So O_RDONLY:

000 000 000 000  (0) 

is ORed with O_NONBLOCK:

100 000 000 000  (04000 in octal notation)

The result is therefore:

100 000 000 000  (0400)

Not a very exciting example, but that's what it is doing...

Lee Netherton
  • 21,347
  • 12
  • 68
  • 102
2

This is the bitwise OR operator. It takes the individual bits in O_RDONLY and combines them with the bits in O_NONBLOCK, and returns the combined value.

For example, suppose the binary value for O_RDONLY is 11001100 and the binary value for O_NONBLOCK is 00001111. OR-ing these together yields 11001111.

John Dibling
  • 99,718
  • 31
  • 186
  • 324
1

That's the bitwise OR operation.

Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
1

That is a bitwise OR. It takes the binary representation of the two arguments (O_RDONLY and O_NONBLOCK) and applies the OR operation to them, returning the result.

Joshua Weinberg
  • 28,598
  • 2
  • 97
  • 90