linux - I am looking at some C functions. What do these bitwise ORs mean?
仅有的幸福
仅有的幸福 2017-05-16 13:29:54
0
2
697

I am learning C recently, and I saw that some functions use bitwise or parameter passing. I don’t know what it means, and it seems that bit operations are rarely used in daily work.

For example, the following piece of code

#define LOCKFILE "/var/run/gwyydaemon.pid"

#define LOCKMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
 fd = open(LOCKFILE,O_RDWR|O_CREAT,LOCKMODE);

What do the O_RDWR|O_CREAT and S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH here mean?

仅有的幸福
仅有的幸福

reply all(2)
漂亮男人

You need to check the documentation yourself

  • O_RDWR: Readable and writable

  • O_CREAT : If the file does not exist, create it

Bitwise OR means combined configuration, assuming (I don’t know the specific value):
O_RDWR 等于二进制 00000001
O_CREAT 等于二进制 00000010 is equal to binary 00000001

O_CREAT is equal to binary 00000010

Then the combined configuration is🎜
00000001  可读写
00000010  创建
--------  位或
00000011  可读写+创建
曾经蜡笔没有小新

It’s just bit operations
S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH and so on should be defined in a certain header file
For example (the value is written casually by me, please check that header file for details, I’m being lazy)

#define S_IROTH 0x01
#define S_IRGRP 0x02
#define S_IWUSR 0x04
#define S_IRUSR 0x08

Wait.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template