博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Bit manipulation
阅读量:4074 次
发布时间:2019-05-25

本文共 1405 字,大约阅读时间需要 4 分钟。

When should you use bitwise operators?

Bitwise operators are used forsaving more space orsaving some time. There are also times when you need to use bitwise operators: if you're working with compression or some forms of encryption, or if you're working on low-level device control, error detection and correction algorithms, data compression, encryption algorithms, and optimizationBit manipulation, in some cases,can obviate or reduce the need to loop over a data structure and can give many-fold speed upsbut the code can become rather more difficult to write and maintain. 

How many basic bitwise operations are there?

Source code that does bit manipulation usually makes use of the basic bitwise operations: AND, OR, XOR, NOT, and bit shifts
As a reminder, here are the 
truth tables
:
AND: 1 & 1 = 1, else are all 0
OR: 0 | 0 = 0, else are all 1
XOR: 1 ^ 0 = 0 ^ 1 = 1, else are 0 
NOT: !0 = 1, !0 = 1
Left Shifts: 0001 << 1 = 0010, general bit_arg<<shift_arg equivalent to multiplication by 2^shift_arg
Right Shifts: 0010 >> 1 = 0001, general bit_arg>>shift_arg equivalent to division by 2^shift_arg

Some useful operations when coding.

Set union(并集): A | B
Set intersection(交集): A & B
Set subtration(集减法): A & ~B
Set negation(集否定): ALL_BITS_ONE ^ A
Assign 2^bit to be 1(指定2^位为1): A |= 1<<bit
Assign 2^bit to be 0(指定2^位为0): A &= ~(1<<bit)
Check if 2^bit is 1(检查2^位为1): (A & 1<<bit) != 0

References:

转载地址:http://ppuni.baihongyu.com/

你可能感兴趣的文章
《软件体系结构》 练习题
查看>>
《数据库系统概论》 第一章 绪论
查看>>
《数据库系统概论》 第二章 关系数据库
查看>>
《数据库系统概论》 第三章 关系数据库标准语言SQL
查看>>
SQL语句(二)查询语句
查看>>
SQL语句(六) 自主存取控制
查看>>
《计算机网络》第五章 运输层 ——TCP和UDP 可靠传输原理 TCP流量控制 拥塞控制 连接管理
查看>>
堆排序完整版,含注释
查看>>
二叉树深度优先遍历和广度优先遍历
查看>>
生产者消费者模型,循环队列实现
查看>>
PostgreSQL代码分析,查询优化部分,process_duplicate_ors
查看>>
PostgreSQL代码分析,查询优化部分,canonicalize_qual
查看>>
PostgreSQL代码分析,查询优化部分,pull_ands()和pull_ors()
查看>>
IA32时钟周期的一些内容
查看>>
获得github工程中的一个文件夹的方法
查看>>
《PostgreSQL技术内幕:查询优化深度探索》养成记
查看>>
PostgreSQL查询优化器详解之逻辑优化篇
查看>>
STM32中assert_param的使用
查看>>
C语言中的 (void*)0 与 (void)0
查看>>
vu 是什么
查看>>