C Primer Plus (15th)

Source: Internet
Author: User

15.1 binary number, bit, and byte

A number represented by a base of 2 is called a binary number. A binary number can be used to represent any integer as a combination of 1 and 0. This system is very suitable for digital computers.

15.1.1 binary integer

The 8-bit bytes used to describe the memory chip and data transmission rate.
The minimum binary number is 00000000, or a simple 0. The number of bytes that can be stored ranges from 0 to 255.
By changing the interpretation of the ing mode, a byte can store integers from-128 to + 127.

15.1.2 signed integer

The representation of the number of symbols is determined by hardware, not by C. The simplest way is to retain the number symbol in 1 bits.
However, this method has a confirmation that there are two zeros + 0 and-0.
Binary complement avoids this problem.
The difference between the two methods is to determine the negative value. Subtract a negative number from a 9-digit combination of 100000000, and the result is the number of negative values.
For example, the bitwise combination of a negative number is 10000000, and the value is 100000000-10000000, that is, 10000000, so the number is-128. Similarly, 128 is-127,111111 is-1.

15.1.3 binary floating point number

Floating point numbers are stored in two parts: A binary decimal point and a binary index.
Many decimal places like 1/3 cannot be exactly expressed in decimal notation. Similarly, many decimals cannot be exactly expressed using binary notation.
To represent a floating point number in a computer, you need to set aside several digits to store a binary decimal number, and the other digits to store an index.

15.2 C Operator

15.2.1 bit logical operators

Four bitwise operators are used to integer data, including char. These operators are called bitwise operators because they operate on each user without affecting the bitwise between the left and right sides.

I. Binary anticode or bitwise inversion :~

Unary operator ~ Change each 1 to 0, and each 0 to 1.

Ii. bitwise AND :&

Binary operator & a new value is generated by comparing two operands by bit. For each bit, the result is 1 only when the corresponding bits of the two operands are 1.

3. bitwise OR: |

Binary operator & a new value is generated by comparing two operands by bit. For each bit, if the corresponding bit in any of the operands is 1, the result is 1.

4. bitwise OR ^

Binary operator & a new value is generated by comparing two operands by bit. For each bit, if one of the corresponding bits in the operand is 1 (but not 1), the result is 1.

15.2.2 usage: mask

The bitwise AND operator is usually used with the mask. A mask is a combination of BITs with some bits set as on and some bits set as off.
Assume that the defined symbolic constant MASK is 2, that is, 00000010 of the binary value, and only bit 1 is non-zero.
Then, flags & = MASK; this statement will cause all the bits except bit 1 to be set to 0.
Because the zero in the mask overwrites the corresponding bit in flags, the process becomes "using the mask ".

15.2.3 usage: Enable a bit

Sometimes, you may need to open a specific bit in a value and keep the other bit unchanged.
For example, if the bit 1 of a MASK is set to 1, set the bit 1 of flags to 1 in the following statement and keep all other bits unchanged: flags | = MASK;

15.2.4 usage: Disable a bit

Sometimes, you may need to disable a specific bit in a value and keep the other bit unchanged.
For example, if the position 1 of a MASK is set to 1, set the bit 1 in flags to 0 in the following statement and keep all other bits unchanged: flag & = ~ MASK;

15.2.5 usage: transpose

Transpose a single bit indicates that if this bit is opened, it will be closed; otherwise, it will be opened.
In this value, the bit with the corresponding mask bit being 1 is transposed, And the bit with the corresponding mask bit being 0 is not changed. Flag ^ = MASK;

15.2.6 usage: view the value of one digit

Assume that you want to view the value of a bit. For example, is the flag bit 1 1 1?
If (flag & MASK) = MASK)

15.2.7 Shift Operator

1. Move left

Left shift operator <moves each of the values in the left operand to the left, and the number of digits to be moved is specified by the right operand. The blank bit is filled with 0, and the bit removed from the end of the left operand is discarded. (10001010) <2 ---- 00101000

2. Right Shift

Left shift operator <moves each of the values in the left operand to the left, and the number of digits to be moved is specified by the right operand. Discard the right bit of the right operand. For the unsigned type, use 0 to fill the left blank bit. For signed types, the result depends on the machine.

Iii. Usage: Shift Operator

Shift operations provide fast and efficient multiplication and division of the power of 2.
Number <n // number multiplied by the n power of 2
Number> n // if the number is not negative, the number is divided by the n power of 2.
For example, if you want to use an unsigned long value to represent the color value, the low byte stores the red brightness, the next byte stores the green brightness, and the third byte stores the blue brightness. Assume that you want the brightness of each color to be stored in the unsigned char variable.
# Define BYTE_MASK 0xff
Unsigned long color = 0x002a162f;
Unsigned char blue, green, red;
Red = color & BYTE_MASK;
Green = (color> 8) & BYTE_MASK;
Blue = (color> 16) & BYTE_MASK;

Iv. Usage: reverse the last n digits in a value

Int invert_end (int num, int bits)
{
Int mask = 0;
Int bitval = 1;
While (bits --> 0)
{
Mask | = bitval;
Bitval <1;
}
Return num ^ mask;
}

15.3 characters

The second method for bitwise operations is to use a bit field, which is a group of adjacent bits in a signed int or unsigned int. A bit field is created by a structure declaration that provides tags for each field and determines the width of the field.
For example, the following statement creates four 1-bit fields:
Struct {
Unsigned int autfd: 1;
Unsigned int bldfc: 1;
Unsigned int undln: 1;
Unsigned int itals: 1;
} Prnt;
You can use the regular structure member operator to assign a value to a separate field, for example, prnt. itals = 0;
Because each field is exactly one bit, 1 and 0 are the only values that can be used for value assignment. The variable prnt is stored in an Int-sized storage unit, but only four of them are used in this example.
You can also create fields of different bit sizes.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.