Bit arithmetic
All the numbers in the program are stored in binary form in the computer's memory. A bitwise operation is a direct manipulation of an integer in-memory bits.
bitwise operations-Definition
In many system programs, it is often required to perform operations or processing at the bit level. The C language provides the function of bit arithmetic, which makes C language can also be used to write system program like assembly language.
using bit arithmetic to solve problems :
Description:
The first line enters the number n (n<=50), which indicates that there are n sets of test cases, the 2nd to the line n+1 each line input m (M is an integer), the statistic and output m in binary representation, the number of 1.
For example: m=9, binary is represented as 1001, then output 2.
#include <stdio.h>intBitcount (intx) {intCount =0; while(X! =0) {x&= (x1); Count++; } returncount;} intMain () {intnum; intx; scanf ("%d", &num); while(num--) {scanf ("%d", &x); printf ("%d\n", Bitcount (x)); } return 0;} Bitwise operations-Operators
The bitwise operator C language provides six bitwise operators:
& Bitwise AND
| Bitwise OR
^ Bitwise XOR OR
~ Take counter
<< left Shift
>> Right Shift
1. Bitwise AND Operation bitwise AND operator "&" are binocular operators. Its function is to participate in the operation of the two number of the corresponding binary phase. Only the corresponding two binaries are 1 o'clock, the result bit is 1, otherwise 0. The number of participating operations appears in a complementary fashion.
For example: The 9&5 can be written as follows: 00001001 (9 twos complement) &00000101 (5 's twos complement) 00000001 (1 of the twos complement) is visible 9&5=1.
Bitwise AND operations are usually used to clear some bits by 0 or to preserve certain bits. For example, A's high eight bits clear 0, reserved low eight bits, can be used as a&255 operation (255 binary number is 0000000011111111).
Main () {
int a=9,b=5,c;
c=a&b;
printf ("a=%d\nb=%d\nc=%d\n", a,b,c);
}
2. Bitwise OR operation bitwise OR operator "|" is the binocular operator. Its function is to participate in the operation of the two number of the corresponding binary phase or. As long as the corresponding two binary has one for 1 o'clock, the result bit is 1. The two numbers participating in the operation are in complement.
For example: 9|5 can be written as follows: 00001001|00000101
00001101 (decimal 13) Visible 9|5=13
Main () {
int a=9,b=5,c;
c=a|b;
printf ("a=%d\nb=%d\nc=%d\n", a,b,c);
}
3. Bitwise XOR or the bitwise XOR operator "^" is the binocular operator. Its function is to participate in the operation of the two number of the corresponding binary dissimilarity or, when the two corresponding binary differences, the result is 1. Participation in the arithmetic is still in complement, for example 9^5 can be written as follows: 00001001^00000101 00001100 (decimal 12)
Main () {
int a=9;
a=a^15;
printf ("a=%d\n", a);
}
4. Negation operation negation Operator ~ is a monocular operator with right-associative. Its function is to reverse the bitwise negation of the number of participating operations. For example, the operation of the: ~ (0000000000001001) result is: 1111111111110110
5. Left shift operator "<<" is the binocular operator. Its function is to shift all the binary of the left operand of "<<" to a number of bits, specifying the number of bits moved by the number of "<<" to the right.
High drop, low 0. For example: A<<4 refers to moving the binary of a to the left by 4 bits. such as a=00000011 (decimal 3), 00110000 (decimal 48) After moving left 4 bits. 6. Right-shift operation right-shift operator ">>" is the binocular operator. The function is to shift all the binary of the left operand of ">>" to the right of several bits, and the number to the right of ">>" to specify the number of bits to move.
For example: Set a=15,a>>2 to move 000001111 right to 00000011 (decimal 3). It should be stated that, for the signed number, when moving right, the symbol bit is moved along with it. When positive, the highest bit is 0, while negative, the sign bit is 1, the highest bit is 0 or the complement 1 depends on the requirements of the compilation system. Turbo C and a number of systems are required to complement 1.
Main () {
unsigned A, B;
printf ("Input a number:");
scanf ("%d", &a);
b=a>>5;
b=b&15;
printf ("a=%d\tb=%d\n", A, b);
}
Please look at one more example!
Main () {
Char a= ' A ', b= ' B ';
int p,c,d;
P=a;
p= (p<<8) |b;
d=p&0xff;
C= (P&0XFF00) >>8;
printf ("a=%d\nb=%d\nc=%d\nd=%d\n", a,b,c,d);
}
Bit field
While some information is stored, it does not need to occupy a full byte, only a few or one bits. For example, when storing a switching volume, there are only 0 and 12 states, with one binary. In order to save storage space and make processing simple, C language also provides a data structure, called "bit field" or "bit segment". The so-called "bit field" is to divide the binary in one byte into several different regions and describe the number of bits per region. Each domain has a domain name that allows operations to be performed by domain name in the program. This allows you to represent several different objects in a bits field of one byte. The definition of a bit field and the description of a bit-field variable are similar to the structure definition, in the form of:
struct bit domain structure name
{bit field List};
Where the list of bit fields is in the form: type specifier bit domain name: bit field length
For example:
struct BS
{
int a:8;
int b:2;
int c:6;
};
The description of a bit-field variable is the same as the structure variable description. Can be defined by the first description, at the same time define the description or direct description of the three ways. For example:
struct BS
{
int a:8;
int b:2;
int c:6;
}data;
Indicates that data is a BS variable, which accounts for two bytes. Where bit domain A occupies 8 bits, bit domain B is 2 bits, bit field C is 6 bits. There are several explanations for the definition of bit fields:
1. A bit field must be stored in the same byte and cannot span two bytes. If one byte has enough space left to hold another domain, the bit field should be stored from the next cell. You can also intentionally make a field start from the next unit. For example:
struct BS
{
unsigned a:4
unsigned:0/* airspace */
Unsigned b:4/* Start storage from the next unit */
unsigned c:4
}
In this bit domain definition, a occupies the first byte of 4 bits, the latter 4 bits fill 0 means not to use, B starts with the second byte, occupies 4 bits, and C occupies 4 bits.
2. Because bit fields are not allowed to span two bytes, the length of the bit field cannot be greater than the length of one byte, that is, it cannot exceed the 8-bit binary.
3. A bit field can have no bit domain name, it is only used to fill or adjust the location. Nameless bit fields are not available. For example:
struct k
{
int a:1
Int:2/* The 2-bit cannot be used */
int B:3
int C:2
};
From the above analysis, we can see that the bit field is essentially a type of structure, but its members are assigned by binary.
The use of bit fields in bit fields is the same as the use of the struct members, and the general form is: bit field variable name • Bit domain name field allows output in various formats.
Main () {
struct BS
{
unsigned a:1;
unsigned b:3;
unsigned c:4;
} bit,*pbit;
Bit.a=1;
bit.b=7;
bit.c=15;
printf ("%d,%d,%d\n", bit.a,bit.b,bit.c);
pbit=&bit;
pbit->a=0;
pbit->b&=3;
pbit->c|=1;
printf ("%d,%d,%d\n", pbit->a,pbit->b,pbit->c);
}
In the example program, the bit-domain structure BS is defined, and the three-bit domain is a,b,c. The variable bit for the BS type and pointer variable pbit to the BS type are described. This means that a bit field can also use pointers.
The 9, 10, and 113 lines of the program assign values to the three bit fields respectively. (Note that the assignment cannot exceed the allowed range of this bit field) the 12th line of the program outputs the contents of three fields in an integer format. The 13th line sends the address of bit field variable bit to pointer variable pbit. The 14th row assigns a value to bit field A as a pointer to 0. Line 15th uses the compound bitwise operator "&=", which is equivalent to: pbit->b=pbit->b&3 bit field B has a value of 7, and 3 for bitwise AND operation results of 3 (111&011=011, decimal value 3). Similarly, the 16th line of the program uses the compound bit operation "|=", equivalent to: pbit->c=pbit->c|1 its result is 15. The 17th line of the program outputs the values of these three fields in a pointer manner.
Type-defined typedef
The C language not only provides a rich data type, but also allows the user to define the type descriptor themselves, which means that the user is allowed to "alias" the data type. The type definition typedef can be used to complete this function. For example, there is an integer, A, B, which is described as follows: int aa,b; where int is the type specifier for an integer variable. int is the complete notation of integer,
In order to increase the readability of the program, the integer specifier can be defined with a typedef as: TypeDef int INTEGER which can be used as an integer to replace int as the type description of the integer variable. For example: INTEGER A, B; it is equivalent to: int A, B; The use of typedef to define arrays, pointers, structures and other types will bring great convenience, not only makes the program simple and makes the meaning more explicit, thus enhancing readability. For example:
typedef char NAME "20"; Indicates that name is a character array type with an array length of 20.
You can then use the name to describe the variable, such as: Name A1,a2,s1,s2, which is exactly equivalent to: Char A1 "20", A2 "20", S1 "20", S2 "20"
Another example:
typedef struct stu{char name "20";
int age;
char sex;
} STU;
Define the structure type that STU represents STU, and then use STU to describe the structure variable: STU body1,body2;
The general form of typedef definition is: TypeDef primitive type name new type name where the original type name contains the definition part, the new type name is generally capitalized, in order to
Easy to distinguish. Macro definitions are sometimes used instead of TypeDef, but macro definitions are done by preprocessing, and TypeDef is done at compile time, which is more flexible and convenient.
Bit Operations Summary (encyclopedia)