'Which of these answers are the equivalent of O_RDWR on Ubuntu 14.04 LTS?

I have been given a list of the following check box containing all possible solutions to the question of choosing all true answers equivalent to O_RDWR on Ubuntu 14.04 LTS. according to this list below

O_RDONLY

1

2

3

1 << 1

3 & 2

3 | 2

O_WRONLY

(O_RDONLY + O_WRONLY)

(O_RDONLY | O_WRONLY)

(O_RDONLY & O_WRONLY)

(O_RDONLY && O_WRONLY)

(O_RDONLY << 1)

(O_WRONLY << 1)

0

I have chosen 2 and (O_RDONLY | O_WRONLY) but am still getting errors

anyone who can help me with this?



Solution 1:[1]

Look into bitwise operators.

O_RDWR is defined in fcntl.h and is equals to 2.
O_WRONLY = 1
O_RDONLY = 0

1 = 1
2 = 2
3 = 3
1 << 1 = 2
3 & 2 = 2
3 | 2 = 3
O_WRONLY = 1
(O_RDONLY + O_WRONLY) = 1
(O_RDONLY | O_WRONLY) = 1
(O_RDONLY & O_WRONLY) = 0
(O_RDONLY && O_WRONLY) = 0
(O_RDONLY << 1) = 1
(O_WRONLY << 1) = 2
0 = 0

Solution 2:[2]

None of them.

The only equivalent to O_RDWR is O_RDWR. O_RDWR is not a bitwise combination of individual bits. Neither are O_EXEC, O_RDONLY, O_SEARCH, or O_WRONLY.

Per the POSIX documentation for open() (bolding mine):

... Applications shall specify exactly one of the first five values (file access modes) below in the value of oflag:

O_EXEC
    Open for execute only (non-directory files). The result is unspecified if this
    flag is applied to a directory.
O_RDONLY
    Open for reading only.
O_RDWR
    Open for reading and writing. The result is undefined if this flag is applied to a FIFO.
O_SEARCH
    Open directory for search only. The result is unspecified if this flag is
    applied to a non-directory file.
O_WRONLY
    Open for writing only.

Solution 3:[3]

O_WRONLY = 1, O_RDONLY = 0 and O_RDWR = 2 so do the bitwise and find which statement gives you 2

have you got it it means 2, 1<<1, 3&2 and (O_WRONLY<<1) these can give you 2

Solution 4:[4]

2, 1<<1 also 00000001 shift left by 1 = 00000010 so, the value also 2 , 3&2 also 2, O_WRONLY << 1 also 2, so this is the answer

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Andrew Henle
Solution 3 Maxwell
Solution 4 GIRMA TAREKEGN