《The C Programming Language》学习笔记——位运算

C语言提供了6个用于位操作的运算符,这些运算符只能作用于整数分量,即只能作用于有符号和无符号的char、short、int和long类型。

&   —— 屏蔽某些位
|     —— 打开某些位
^    —— 在两个运算分量的对应位不相同时置该位为1,否则,置该位为0。
<< —— 将左运算分量左移由右运算分量所指定的位数
>> —— 将左运算分量右移由右运算分量所指定的位数
~   —— 求整数的反码

Tips:

1.     我们常用移位取反等操作的组合求得最后结果。

如:将 x从第p位开始的n位置为“指定”的值。
设 n=3,p=4,原题目即为对x的第4位开始的后3位进行操作。
则常用到的操作元素有
 ~(~0 << n)<<(p+1-n) == 0000011100
~(~(~0 << n)<<(p+1-n)) ==1111100011

2.在求反中,异或求反非常重要,^运算符运算规律有:
1 ^ 1 = 0 ;      1 ^ 0 = 1
x ^ 1 = ~x ;     x ^ 0 = x

3& ~”去掉某些属性。VC++中窗口去掉最大化按钮
WS_OVERLAPPED & ~ WS_MAXIMIZE     

4.我们在移位运算时还常会用到wordlength函数
函数:wordlength
功能:计算运行程序的计算机所使用的字长
int  wordlength( void )
{
      
int i;
      unsigned v 
= (unsigned)~0;

           
for (i = i; (v = v >> 1> 0; i++)
            ;
     
return i;
}




范例程序
 
1.    函数: setbits(x p, n, y)
 功能:x从第p位开始的
n
位的值被置为y的最右边n位的值,其余各位保持不变。

unsigned setbits(unsigned x,  int  p,  int  n, unsigned y)
{
       
return x & ~(~(~0 << n)<<(p+1-n)) |
              y
<<(p+1-n) & ~(~0 << n)<<(p+1-n);
              
}
 

2.    函数:invert(x,p,n)
 功能x从第p位开始的n位被求反。
unsigned invert(unsigned x,  int  p,  int  n)
{
       
return (x |~(~0<<n)<<(p+1-n)) &
        
~(x & ~(~0 << n)<<(p+1-n));
}


3 .函数: rightrot(x, n)
    
功能:将x向右循环移动n位所得到的值。

unsigned rightrot(unsigned x,  int  n)
{
    
int wl;
 
    wl 
= wordlength();
 
    
return x>>| ((x & ~(~0<<n))<<(wl-n));
}
The C programming Language By Brian W. Kernighan and Dennis M. Ritchie. Published by Prentice-Hall in 1988 ISBN 0-13-110362-8 (paperback) ISBN 0-13-110370-9 目录结构: Contents Preface Preface to the first edition Introduction Chapter 1: A Tutorial Introduction Getting Started Variables and Arithmetic Expressions The for statement Symbolic Constants Character Input and Output File Copying Character Counting Line Counting Word Counting Arrays Functions Arguments - Call by Value Character Arrays External Variables and Scope Chapter 2: Types, Operators and Expressions Variable Names Data Types and Sizes Constants Declarations Arithmetic Operators Relational and Logical Operators Type Conversions Increment and Decrement Operators Bitwise Operators Assignment Operators and Expressions Conditional Expressions Precedence and Order of Evaluation Chapter 3: Control Flow Statements and Blocks If-Else Else-If Switch Loops - While and For Loops - Do-While Break and Continue Goto and labels Chapter 4: Functions and Program Structure Basics of Functions Functions Returning Non-integers External Variables Scope Rules Header Files Static Variables Register Variables Block Structure Initialization Recursion The C Preprocessor File Inclusion Macro Substitution Conditional Inclusion Chapter 5: Pointers and Arrays Pointers and Addresses Pointers and Function Arguments Pointers and Arrays Address Arithmetic Character Pointers and Functions Pointer Arrays; Pointers to Pointers Multi-dimensional Arrays Initialization of Pointer Arrays Pointers vs. Multi-dimensional Arrays Command-line Arguments Pointers to Functions Complicated Declarations Chapter 6: Structures Basics of Structures Structures and Functions Arrays of Structures Pointers to Structures Self-referential Structures Table Lookup Typedef Unions Bit-fields Chapter 7: Input and Output Standard Input and Output Formatted Output - printf Variable-length Argument Lists Formatted Input - Scanf File Access Error Handling - Stderr and Exit Line Input and Output Miscellaneous Functions String Operations Character Class Testing and Conversion Ungetc Command Execution Storage Management Mathematical Functions Random Number generation Chapter 8: The UNIX System Interface File Descriptors Low Level I/O - Read and Write Open, Creat, Close, Unlink Random Access - Lseek Example - An implementation of Fopen and Getc Example - Listing Directories Example - A Storage Allocator Appendix A: Reference Manual Introduction Lexical Conventions Syntax Notation Meaning of Identifiers Objects and Lvalues Conversions Expressions Declarations Statements External Declarations Scope and Linkage Preprocessor Grammar Appendix B: Standard Library Input and Output: <stdio.h> File Operations Formatted Output Formatted Input Character Input and Output Functions Direct Input and Output Functions File Positioning Functions Error Functions Character Class Tests: <ctype.h> String Functions: <string.h> Mathematical Functions: <math.h> Utility Functions: <stdlib.h> Diagnostics: <assert.h> Variable Argument Lists: <stdarg.h> Non-local Jumps: <setjmp.h> Signals: <signal.h> Date and Time Functions: <time.h> Implementation-defined Limits: <limits.h> and <float.h> Appendix C: Summary of Changes
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值