有意义的位操作
非
~ 0 = 1
~ 1 = 0
可以用于整体翻转.
~ 1000 = 0111
~ 0100 = 1011
或
0 | 0 = 0
0 | 1 = 1
1 | 0 = 1
1 | 1 = 1
或,有真就是真.
x | 0 = x
x | 1 = 1
和0或是原值,和1或为1.可以用于打开
某位的开关.
0011 | 1000 = 1011 # 打开本来关闭的
1011 | 1000 = 1011 # 打开本来打开的
与
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1
与,都是真才真.
x & 0 = 0
x & 1 = x
和0与是0,和1与是原值.可以用于关闭
某位的开关.
0011 & (~1000) = 0011 & 0111 = 0011 # 关闭本来关闭的
1011 & (~1000) = 1011 & 0111 = 0011 # 关闭本来打开的
异或
0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 0 = 1
1 ^ 1 = 0
相同为零,不同为1.
x ^ 0 = x
x ^ 1 = (~x)
和0异或是本身,和1异或是取反.可以用来翻转某个位.
0011 ^ 0100 = 0111
0011 ^ 0000 = 0011
0011 ^ 1111 = 1100
例子
#include "apue.h"
#include <fcntl.h>
int main(int argc, const char * argv[]) {
struct stat mStat;
const char * file_path = "hi.txt";
if(stat(file_path, &mStat) <0){
err_sys("stat获取出错!\n");
}
// 在原有权限的基础上,关闭Other的RWX,打开User的RWX,设置用户ID位
chmod(file_path, (mStat.st_mode & ~S_IRWXO) | S_IRWXU | S_ISUID);
exit(0);
}
Baodong-MBP:chmod cbd$ chmod 0477 hi.txt
Baodong-MBP:chmod cbd$ ll hi.txt
-r--rwxrwx 1 cbd staff 11 8 23 14:36 hi.txt*
Baodong-MBP:chmod cbd$ ./a.out
Baodong-MBP:chmod cbd$ ll hi.txt
-rwsrwx--- 1 cbd staff 11 8 23 14:36 hi.txt*
参考
/*
* Copyright (c) 2003-2012 Apple Inc. All rights reserved.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. The rights granted to you under the License
* may not be used to create, or enable the creation or redistribution of,
* unlawful or unlicensed copies of an Apple operating system, or to
* circumvent, violate, or enable the circumvention or violation of, any
* terms of an Apple operating system software license agreement.
*
* Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_END@
*/
/*
* [XSI] The symbolic names for file modes for use as values of mode_t
* shall be defined as described in <sys/stat.h>
*/
#ifndef S_IFMT
/* File type */
#define S_IFMT 0170000 /* [XSI] type of file mask */
#define S_IFIFO 0010000 /* [XSI] named pipe (fifo) */
#define S_IFCHR 0020000 /* [XSI] character special */
#define S_IFDIR 0040000 /* [XSI] directory */
#define S_IFBLK 0060000 /* [XSI] block special */
#define S_IFREG 0100000 /* [XSI] regular */
#define S_IFLNK 0120000 /* [XSI] symbolic link */
#define S_IFSOCK 0140000 /* [XSI] socket */
#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
#define S_IFWHT 0160000 /* OBSOLETE: whiteout */
#endif
/* File mode */
/* Read, write, execute/search by owner */
#define S_IRWXU 0000700 /* [XSI] RWX mask for owner */
#define S_IRUSR 0000400 /* [XSI] R for owner */
#define S_IWUSR 0000200 /* [XSI] W for owner */
#define S_IXUSR 0000100 /* [XSI] X for owner */
/* Read, write, execute/search by group */
#define S_IRWXG 0000070 /* [XSI] RWX mask for group */
#define S_IRGRP 0000040 /* [XSI] R for group */
#define S_IWGRP 0000020 /* [XSI] W for group */
#define S_IXGRP 0000010 /* [XSI] X for group */
/* Read, write, execute/search by others */
#define S_IRWXO 0000007 /* [XSI] RWX mask for other */
#define S_IROTH 0000004 /* [XSI] R for other */
#define S_IWOTH 0000002 /* [XSI] W for other */
#define S_IXOTH 0000001 /* [XSI] X for other */
#define S_ISUID 0004000 /* [XSI] set user id on execution */
#define S_ISGID 0002000 /* [XSI] set group id on execution */
#define S_ISVTX 0001000 /* [XSI] directory restrcted delete */
#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
#define S_ISTXT S_ISVTX /* sticky bit: not supported */
#define S_IREAD S_IRUSR /* backward compatability */
#define S_IWRITE S_IWUSR /* backward compatability */
#define S_IEXEC S_IXUSR /* backward compatability */
#endif
#endif /* !S_IFMT */