码迷,mamicode.com
首页 > 其他好文 > 详细

Bitwise Operators

时间:2020-05-19 12:14:52      阅读:62      评论:0      收藏:0      [点我收藏+]

标签:VID   rds   expr   under   HERE   constant   ima   called   operation   

C and C++ allow various types of operators.  By now, you should be familiar with the basic binary operators +, -, *, / and the boolean operators <, >, <=, >=, &&, || and so on.  Another type of operators that are used to manipulate individual bits in variables are called Bitwise operators.

In order to understand bitwise operators, one must first understand how the bits in a byte, short, int or long represent an integer.  In order to make this presentation easier to follow, it will discuss operations on bytes, or variables that are 8 bits long.

Recall that bits are binary and can be represented by either a 0 or a 1.  A byte is a group of 8 bits, where the most significant bit is in the leftmost position.  Thus, the string of bits 00000000 represent the value 0 (zero), the string of bits 00000001 represent the value 1, 00000010 represent the value 2, 00000011 represents the value 3 and so on.

Some bitwise operators work in a manner similar to boolean operators, such that True AND False is False, True OR False is True, NOT True is False.  When working with bits, 0 represents false, and 1 represents true.  The bitwise operator for AND is &, for OR is |, and NOT is ~.  When using bitwise operators for AND, OR, and NOT, the operator works on each bit in the expression.  In other words, if the bit string 01010101 is OR‘ed with bit string 10101010, the result is bit string 11111111.  When written in C code, the preceding operation would look something like this:

Bitwise OR example:
unsigned char a = 85;    // 01010101 in binary, 0125 in octal or 0x55 in hex
unsigned char b = 170;   // 10101010 in binary, 0252 in octal or 0xAA in hex
unsigned char c = a | b; // c = 11111111 in binary, 0377 in octal, 0xFF in hex or 255 in decimal

A bitwise AND example, using the above values would be:
unsigned char a = 85;    // 01010101 in binary, 0125 in octal or 0x55 in hex
unsigned char b = 170;   // 10101010 in binary, 0252 in octal or 0xAA in hex
unsigned char c = a & b; // c = 00000000 in binary, 0000 in octal, 0x00 in hex or 0 in decimal

And a bitwise NOT example:
unsigned char a = 85;   // 01010101 in binary, 0125 in octal or 0x55 in hex
unsigned char c = ~a;   // c = 10101010 in binary, 0xAA in hex or 170 in decimal

There is also a bitwise Exclusive OR, or XOR operator represented by a caret (^).  Exclusive OR states that if both bits are the same, the result is 0.  However, if both bits are different, the result is 1:
unsigned char a = 85;    // 01010101 in binary, 0125 in octal or 0x55 in hex
unsigned char b = 170;   // 10101010 in binary, 0252 in octal or 0xAA in hex
unsigned char c = 204;   // 11001100 in binary, 0314 in octal or 0xCC in hex
unsigned char d = a ^ b; // d = 11111111 in binary, 0377 in octal, 0xFF in hex 01010101 in binary, 0125 in octal or 0x55 in hexor 255 in decimal
unsigned char e = a ^ c; // e = 10011001 in binary, 0231 in octal, 0x99 in hex or 153 in decimal

These operations can be performed constants as well as variables. The last statement in each example given above could also have been written as:
unsigned char c = 85 | 170;
unsigned char c = a & 0xAA;
unsigned char c = ~85;
unsigned char e = 0x55 ^ c;

There are two other bitwise operators you should know about,  << or Left Shift and >> or Right Shift.  These operations have the effect of shifting the bits in a byte (or short, int, long, etc) to the left or right.  Thus, a Left Shift two places on 00001111 results in 00111100.  A Right Shift two places on 00001111 results in 00000011.  Note that when bits are shifted outside of the bit size of the variable (8 bits for a byte), they are lost.  Also note that when a blank space is created by a shift, these blank space are filled with 0s.

Some examples for left and right shift operators are:
unsigned char a = 85;     // 01010101 in binary, 0125 in octal or 0x55 in hex
unsigned char b = a << 5; // b = 10100000 in binary, 0240 in octal, 0xA0 in hex or 160 in decimal
unsigned char c = b >> 3; // c = 00010100 in binary, 0024 in octal, 0x14 in hex or 20 in decimal

Bitwise Operators

标签:VID   rds   expr   under   HERE   constant   ima   called   operation   

原文地址:https://www.cnblogs.com/JasperZhao/p/12916050.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!