The Quick Start bit computing language series -C

Many times we have to deal with bits of issues, such as the number two position by adding a bit of each memory cell to the left or right one, and so on, these problems are often encountered.

C language bitwise operators provided:

We are the following:

- which, in addition to other operators are binary operator, two operations to participate in the data, according to expand bits, then the appropriate operations, such as: "&, OR, XOR, NOT" and the like. Some more applicable bitwise operators in a particular scene, such as:

& Commonly used to clear a cell, takes a certain specified number of bits, reserved bits specify the operation | used to position a certain number of 1 ^ determines two bit values, is different from 1, 0 is the same, common a particular bit to flip the like - is a monocular (RMB) operator, is used to a binary bitwise, i.e. 0 becomes 1 bit is used in combination with other operators used to set the mask character shift operation

<< The Members of a number of all bits left, the upper left overflow discard ineffective. For example: a = << 2 binary number to a two left and right complement 0 if a = 15, i.e., binary number 00001111 left 2 to give 00111100, (decimal 60) >> If the original sign bit is 0 (positive) into the left 0. If the sign bit is 1 (negative), then moved to the left 0 or 1 depending on system, into 0 is called "logical shift right", move called "arithmetic shift right" 1. For example:

a value is octal 113 755: a: 1,001,011,111,101,101 (expressed in binary form) a >>. 1: (logical shift right) 0100101111110110 a >>. 1: 1,100,101,111,110,110 (when an arithmetic right shift) In some systems, a >> 1 have octal number 045,766, while in other systems may get is 145,766. Turbo C and other C compiler uses an arithmetic right shift, i.e., to the right when the number of symbols, if the sign bit of the original 1, into the upper left is a fact, a change value or more binary bits, usually bit manipulation instructions implemented, such as by &, |, ^, ~ a combination thereof, or an assignment of binary bit change its value. Then there is no other way to achieve this function?

Of course, there are other mode of operation, when the introduction of the structure before said "bit field" is also possible to achieve a number one or more bits assigned by bit fields. In the C language, allowing in bits specify its length members share a memory structure, such a member in bits called "bit block" or "bit field" (bit field); using bit field is possible with less data storage bits.

Definition of the reference bit segment has few important notes:

Type Bits section members must be designated as signed or unsigned types, which can be char, unsigned char or short, unsigned short. The float is not a test case to see:

When the assignment to the bit segment member, not departing from the scope defined by the bit field, as described above in the example, is defined as a segment member 2, a maximum of 3, i.e. (11), so that data a = 5, will take the lower 5 two assignment, you have not desired value. A bit segment must be stored in a storage unit, the two units can not cross; if the first cell space can not accommodate a bit segment, the unused space, and the next bit segment from a storage unit from a look at an example:

我们假设可以跨位段,那上面的结果应该是8,可能是这样的:

实则不然,其实它是这样的:

位段的长度不能大于存储单元的长度

可以看出来报错了,因为char是占一个字节的存储单元,只有8位,而char a :9;a占9位超过了char存储单元的长度,因此出错。

不能定义位段数组

http://www.wu0553.com/news/37247.html
http://www.wu0553.com/news/37250.html
http://www.wu0553.com/news/37251.html
http://www.wu0553.com/news/37253.html
http://www.wu0553.com/news/37254.html
http://www.wu0553.com/news/37256.html
http://www.wu0553.com/news/37257.html
http://www.wu0553.com/news/37259.html
http://www.wu0553.com/news/37260.html
http://www.wu0553.com/news/37261.html
http://www.wu0553.com/news/37263.html
http://www.wu0553.com/news/37267.html
http://www.wu0553.com/news/37268.html
http://www.wu0553.com/news/37322.html
http://www.wu0553.com/news/37366.html
http://www.wu0553.com/news/37370.html
http://www.wu0553.com/news/37377.html
http://www.wu0553.com/news/37395.html
http://www.wu0553.com/news/37396.html
http://www.wu0553.com/news/37397.html
http://www.wu0553.com/news/37402.html
http://www.wu0553.com/news/37403.html
http://www.wu0553.com/news/37404.html
http://www.wu0553.com/news/37405.html
http://www.wu0553.com/news/37406.html
http://www.wu0553.com/news/37407.html
http://www.wu0553.com/news/37430.html
http://www.wu0553.com/news/37431.html
http://www.wu0553.com/news/37432.html
http://www.wu0553.com/news/37433.html
http://www.wu0553.com/news/37436.html
http://www.wu0553.com/news/37441.html
http://www.wu0553.com/news/37448.html
http://www.wu0553.com/news/37454.html
http://www.wu0553.com/news/37455.html
http://www.wu0553.com/news/37465.html
http://www.wu0553.com/news/37466.html
http://www.wu0553.com/news/37467.html
http://www.wu0553.com/news/37468.html
http://www.wu0553.com/news/37469.html
http://www.wu0553.com/news/37470.html
http://www.wu0553.com/news/37471.html
http://www.wu0553.com/news/37473.html
http://www.wu0553.com/news/37474.html
http://www.wu0553.com/news/37475.html
http://www.wu0553.com/news/37476.html
http://www.wu0553.com/news/37477.html
http://www.wu0553.com/news/37478.html
http://www.wu0553.com/news/37480.html
http://www.wu0553.com/news/37481.html
http://www.wu0553.com/news/37482.html
http://www.wu0553.com/news/37483.html

如一个段要从另一个字开始,可以定义:

unsigned a:1;unsigned b:2;unsigned :0;unsigned c:3;(另一个单元)由于用了长度为0的位段,其作用是使下一个位段从下一个存储单元开始存放;将a、b存储在一个存储单元中,c另存在下一个单元(“存储单元”视不同的编译系统而异).看个例子:

Guess you like

Origin www.cnblogs.com/cc888/p/11672039.html