C ++ binary decimal, understanding of exotic, binary decimal

Source: Internet
Author: User

C ++ binary decimal, understanding of exotic, binary decimal

Note: The default value of 1 byte is 8 digits, with numbers 7 ~ 1.

1. binary decimal

In fact, binary notation can only accurately represent the sum of multiple 1/2 powers, such as 3/4, 7/8, and 1/3, 2/5, and so on, which cannot be accurately expressed.

2. Understanding of exclusive or

Male and female can give birth to children, otherwise they will not. Coexistence is not allowed.

The exclusive or operation is actually a semi-adder operation without carrying

0 xor 0 = 0

0 xor 1 = 1

1 xor 1 = (1) 0

1 xor 0 = 1

The difference or is actually to judge whether the two input logic values are different. If they are different, the result is 1 and the same value is 0. If a = 0, B = 0, a, or B are the same, the output result is 0 and the result is the same as B;

If a = 0, B = 1, a is different, or B is different, the output result is 1, and the result is the same as B;

If a = 1, B = 0, a is different, or B is different, the output result is 1, and the result is opposite to B;

If a = 1, B = 1, a, or B are the same, the output result is 0, and the result is opposite to B;

3. Usage: mask

Bitwise operators are commonly used in masks. Masks refer to bitwise combinations set to on (1) or off (0.

Why is it called a mask?

What happens after we combine a volume with a mask:

For example, assume that the definition of the symbolic constant MAS is 2 (that is, the binary form is 00000010), only 1 is 1, and other bits are 0

Flag = 10010110b

Flags = flags & MARK;

Set all bits except the 1st bit in flags to 0, because the values of the 1st bit and the operator (&) in any combination of BITs and 0 remain unchanged, (If 1 is 1, 1 & 1 gets 1; if 1 is 0, 0 & 1 = 0), this process uses a mask, because 0 in the mask hides the corresponding bit in flags.

In this analogy, we can regard 0 in the MASK as opaque, 1 as transparent, and the expression flags & MASK is equivalent to the bit combination of flags covered by the MASK, which is visible only when the MASK is 1.

4. Usage: Enable a bit (set a bit)

Sometimes, you need to enable the special location in a value while keeping other bits unchanged.

For example, an ibm pc sends a value to the port to control the hardware. For example, to enable the built-in speaker, you must enable the first bit while keeping the other bit unchanged, in this case, you can use the bitwise OR operator (| ).

Flag = 11611b

MARK = 10110110b

Flags | = MARK;

In this way, (00001111) | (10110110) = (10111111)

MARK is 1, flags and their corresponding bits are also 1, MARK is 0, flag and its corresponding BITs remain unchanged.

This method sets the corresponding bit 1 in flags according to the bit marked as 1, and the other bit remains unchanged.

5. Usage: close a bit (clear a bit)

Similar to opening a specific bit, you sometimes need to disable the specified bit without affecting other bits. If you want to disable the 0 bits in the flag, similarly, MARK has only 1 bits (that is, open ):

Flags = flags &~ MARK;

When MARK 1 is set to 1, all other bits are set to 0 ~ MARK except the value of 1 is 0, and all other bits are 1. Use &. Any bits and the value of 1 must be combined. Therefore, this statement keeps the value of 1 unchanged and changes all other bits. In addition, use &, any combination of BITs and 0 requires 0, so no matter what the initial value of 1 is, set it to 0;

For example, if flags is 00001111 and MARK is 10110110, the following expression is used:

Flags &~ MARK

(00001111 )&~ (10110110)

The result is 00001001,

The bits in the MASK are set (cleared) to 0 in the result, and the bits corresponding to the bits marked as 0 in the flags are not changed in the result.

You can use the following simplified form:

Flags & = ~ MARK;

6. Usage: Switch bit

The switch bit refers to enabling or disabling the closed bit. You can use the bitwise XOR or operator (^) to switch the bit.

Flags = flags ^ MASK;

Flags ^ = MASK;

For example, assume that flags is 00001111, and MASK is 10110110,

Flags ~ MASK

(00001111) ^ (10110110 );

The result is:

(10111001)

In flags, the bits corresponding to the MAS 1 phase are switched, and the bits corresponding to the MASK 0 phase remain unchanged.

6. Usage: Check the bit value

Sometimes you need to check a value. For example, is the first digit set to 1 in flags? You cannot directly compare flags and MASK as follows:

If (flags = MASK)

Puts ("WOW") // cannot work properly

In this way, even if the 1 bits of flags are 1, the comparison result is false because the other bits of flags must be overwritten and compared with the MASK.

If (flags & MASK) = MASK)

Puts ("WOW ")

For example, flags = 01010010 MASK = 00000010

After bitwise AND operation, we get 00000010, which is equal to the MASK. Then we can determine that the 1th digit is 1.

Since the bitwise operator has a lower priority than =, parentheses must be added around flags & MASK.

To avoid information passing through the boundary, the mask must be at least the same width as the overwrite value.

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.