`
dato0123
  • 浏览: 917194 次
文章分类
社区版块
存档分类
最新评论

An introduction to bitwise operators

 
阅读更多

读了codeproject上的这篇《An introduction to bitwise operators,前面两个运算符说得不错,但第三个异或运算符感觉不够准确,作者给出的示例不知道有什么用处,不就是把数做了两次异或又回来了么?<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

&运算符用来判定某些位是0还是1
#include<iostream>
usingnamespacestd;
intmain(void)
{
intnum=17;
if(num&0x10)
{
cout
<<"第四位是"<<endl;
}

else
{
cout
<<"第四位是"<<endl;
}

return0;
}

|运算符用来对某些位置1

#include<iostream>
usingnamespacestd;
intmain(void)
{
intnum=50;
num
=num|0x04;
cout
<<hex<<num<<endl;
return0;
}

异或运算符最常用的应该是用其实现两个数的交换:

#include<iostream>
usingnamespacestd;
intmain(void)
{
intn1=17,n2=20;
n1
=n1^n2;
n2
=n2^n1;
n1
=n1^n2;
cout
<<n1<<""<<n2<<endl;
return0;
}


将取反运算符和与运算符结合起来,可以对某些位置零:

#include<iostream>
usingnamespacestd;
intmain(void)
{
intb=50;
cout
<<"b="<<b<<endl;
intc=b&~0x10;
cout
<<hex<<"c="<<c<<endl;
return0;
}

最后的位域有一个问题没搞懂:

#include<iostream>
usingnamespacestd;

structdate_struct
{
intday:4,//1to31
month:4,//1to12
year:12;
}
;

intmain(void)
{
date_structd1;
d1.day
=8;
d1.month
=8;
d1.year
=1844;
cout
<<d1.year<<""<<d1.month<<""<<d1.day<<endl;
return0;
}

我已经设置daymonth各自占据4位,应该是可以满足的,可结果却都是-8why?

分享到:
评论

相关推荐

    The C Cheat Sheet - An Introduction to Programming in C

    4.5 Bitwise Operators 4.6 Relational Operators 4.7 Logical Operators 4.8 The Conditional Operator The C Cheat Sheet Revision 1 Copyright ? 2000 Andrew Sterian iv 4.9 The Comma Operator 4.10 ...

    Programming.in.C.2nd.Edition..0198065280.

    Some advanced features of C such as memory models, command-line arguments, and bitwise operators have also been included. Case studies demonstrating the use of C in solving mathematical as well as ...

    The C programming Language(chm格式完整版)

    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 -...

    ISO_IEC_11172-2 MPEG1标准

    2.2.4 Bitwise Operators 17 2.2.5 Assignment 17 2.2.6 Mnemonics 17 2.2.7 Constants 18 2.3 Method of Describing Bit Stream Syntax 18 2.4 Requirements 20 2.4.1 Coding Structure and Parameters 20 2.4.2 ...

    ISO_IEC_11172-1 System 标准

    2.2.4 Bitwise Operators 17 2.2.5 Assignment 17 2.2.6 Mnemonics 17 2.2.7 Constants 18 2.3 Method of Describing Bit Stream Syntax 18 2.4 Requirements 20 2.4.1 Coding Structure and Parameters 20 2.4.2 ...

    The C programming Language

    Operators and Expressions Variable Names Data Types and Sizes...Bitwise Operators Assignment Operators and Expressions Conditional Expressions Precedence and Order of Evaluation ...

    Practical C++ Programming C++编程实践

    ) The Bitwise Exclusive OR (^) The Ones Complement Operator (NOT) (~) The Left and Right Shift Operators (&gt;) Setting, Clearing, and Testing Bits Bitmapped Graphics Programming Exercises Answers to ...

    Programming in Objective-C 4th Edition

    14 Introduction to the Foundation Framework 303 Foundation Documentation 303 15 Numbers, Strings, and Collections 307 Number Objects 307 String Objects 312 More on the NSLog Function 312 The ...

    C# 语言规格说明(English Edition第五版)

    1. Introduction 1 1.1 Hello world 1 1.2 Program structure 2 1.3 Types and variables 4 1.4 Expressions 6 1.5 Statements 8 1.6 Classes and objects 12 1.6.1 Members 12 1.6.2 Accessibility 13 1.6.3 Type ...

    JavaScript权威指南

    Introduction to JavaScript Section 1.1. JavaScript Myths Section 1.2. Versions of JavaScript Section 1.3. Client-Side JavaScript Section 1.4. JavaScript in Other Contexts Section 1.5. ...

    The C Programming Language 第二版 英文版

    2.9 Bitwise Operators.............................. 2.10 Assignment Operators and Expressions.......... 2.11 Conditional Expressions....................... 2.12 Precedence and Order of Evaluation....

    python3.6.5参考手册 chm

    PEP 372: Adding an Ordered Dictionary to collections PEP 378: Format Specifier for Thousands Separator PEP 389: The argparse Module for Parsing Command Lines PEP 391: Dictionary-Based Configuration...

    Thinking in Java 4th Edition

    Bitwise operators .................. 75 Shift operators ......................76 Ternary if-else operator ......79 String operator + and += .............................. 80 Common pitfalls when using...

    Google C++ International Standard.pdf

    5.12 Operators and punctuators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 5.13 Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...

    C++ 标准 ISO 14882-2011

    2.13 Operators and punctuators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2.14 Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...

    Outline of Programming with C

    1 1.1 Introduction to Computers ............................................................................................. 1 1.2 Computer Characteristics .....................................

Global site tag (gtag.js) - Google Analytics