C/c++/java binary/16 Application: Large number (more than 32 binary digits), bitwise logical operation, attribute value setting, etc.

Source: Internet
Author: User
Tags bitwise
Bitwise Logical Operations, property settings

Bitwise logical operation in the efficient operation of the data to use a lot of high-level language in peacetime, most of the time we do not need to manually set these, because it has been encapsulated in a variety of modules/classes inside.

These operations are worth using when you need to manually define some features.
One thing to be aware of is that all bitwise operations are bits bitwise operations, and if the data is expressed in decimal or hexadecimal notation, it is actually a bitwise operation using binary. by position and: &

0&0=0; 1&0=0; 0&1=0; 1&1=1;

(1 unchanged, 0 becomes 0)
For example: 1001&0011 = 0001
The negative numbers are stored in the computer in the form of complement, so we have to do the bitwise operation according to the complement
Application:
1, position the finger to clear zero. Only needs and refers to the position of 0, the other bit 1 of the number of & operations, these bits are placed 0
2, get the point of positioning. Constructs a binary number, needs to obtain the bit is 1, the other bit is 0, bitwise & operation, the result is the data localization. by bit or: |

0|0=0;   0|1=1;   1|0=1;    1|1=1;

(0 unchanged, 1 becomes 1)
Application:
1, set the specified bit of data to 1. For example, if you want to set the fourth bit to 1, you need to construct a binary number: 1000, and then the data to the budget, then the fourth digit of the data will be set to 1 (regardless of whether it is 1) bitwise XOR or: ^

0^0=0;   0^1=1;   1^0=1;   1^1=0;

(0 unchanged, 1 reversed)
Application:
1, with 1 different or, can flip
2, with 0 XOR or, can retain the original value.
These two points are very clear. Bitwise COUNTER: ~

~1=0;   ~0=1;
application of a bitwise logical Operation: property settings (This should have a scientific name, for the time being do not know how to say)

To set some properties on a project. If the uniqueness of the property is fine, for example, a project, the possible attributes of a,b,c, can only be one of them, then only need to set these property values to a static constant, and then to judge the good.

But many times the attributes that need to be set are stacked. For example, the possible attribute is, a,b,c,d,e,f,g.
Can be one or several, can be AC, can also be DFG, can also be E. And these attributes are not explicitly categorized, most of them are superimposed on the same class of attributes, so they cannot be divided into different attribute values.
Specifically, a window can be a stack of one or several of these attributes, such as foreground/maximized/transparent/windowing/default position/Hold top level, and so on. A file can be a stack of one or several of the files/folders/system files/hidden files/user files , and so on.

At this time, it's not good to judge like a single attribute. The best way to do this is to use a bitwise logical operation .

Suppose there are 8 properties for a project. Then construct a 8-bit binary number (that is, a 2-bit hexadecimal number). Each bit corresponds to a property and is set to a static constant.
a:0000 0001
b:0000 0010
c:0000 0100
d:0000 1000
............
In this way, you can use bitwise logical operations to construct the property value.
For example, my attribute value is ABC, then the constructed value is:a|b|c.
According to the previous description, | The operation will make the corresponding 1 position 1, that is to say, a|b|c = 0000 0111, which is also a unique value, and does not duplicate all 8 possible attributes. Similarly, we can also construct other superposition properties, 0100 1101,1001 0000, and so on.

When judging attributes, you need to use the & operation.
For example, to determine if an item has a property, you can: if ((attr&a) ==a)
According to the above description,& function is: 0 changed to 0,1 unchanged. That is, the value of the attr, corresponding to a 0 in a bit, regardless of whether it is 0, will be programmed 0, corresponding to a 1 of the bit, then remain unchanged.
So, if the attr has a property of a, then its corresponding A-attribute bit will be 1, and it will remain 1, and if there is no this attribute, it is 0, and the operation remains 0.
And the other bits, whether they are 0 or not, will become 0. Therefore, you can use the IF ((Attr&a) ==a) to determine the property.

Similarly, when setting properties, use the | Operation.
Suppose the attr is to be set to ABCD simply: attr = a|b|c|d; You can have a large number (more than 32 binary digits)

C language has already supported 64-bit, has a separate method to operate 64 digits (so far has not been tested), but most of the time we still in the 32-digit operation.

In the absence of support for the 64-digit period, to store a large amount of data, such as file size, need to use a high and low storage method. That is, in the attribute will correspond to two values, one is low 32 bits, one is high 32 bits.

When the amount of data is small, the high is always 0. If the amount of data overflows at low levels, it will be +1 in the high position.
Note that, if directly written out, only binary/16 can be directly expressed as "high-low", such as a number is low 0x1820 ff11, High is 0x1, that means that is 0x1 1820 ff11
If it's a decimal, it needs to be converted, because a high 0x1 is represented as a decimal with a 32-bit maximum value of +1.

If you want to add, you need to add a high/low, if the low generates a carry, then add 1 in the result of the high.
How to judge the low carry.
If it is unsigned, it is simple, because the 32-bit value after the carry is only the 32-bit value, the 33rd bit will be directly ignored by the computer, so if the result is less than any of the added value, it means that the carry is produced.
NumA + NumB = result;
if (Result<numa) ......
The case of signed numbers has not been studied. Just now this method is also found on the Internet, not to see if there are bugs. the high and low level representation is converted into 10

result = high* (Maxdword + 1) + Low
result– true Results
Maximum maxdword–32 digits (0xFFFF FFFF)

So, how does that mean in the computer?
0xFFFF FFFF Decimal is 4 294 967 295,0x1 0000 0000 The actual value is 4 294 967 296, but the computer directly put more than 32 digits, so the computer inside this number is 0x0000 0000, which is the decimal number 0

To represent a number over 32 digits on your computer, you can use the following methods. (The way I want to, I don't know if it's practical)

A character array char bit[20 with a specified number of digits (for example, a 20-bit decimal number);
The characters on each bit of the array, corresponding to the decimal digits of each digit, and then according to the normal algorithm for subtraction operation.
Addition is to add to the bit, if the carry, on the last one plus 1.
Multiplication is similar.
Division has not been carefully thought about, later.

Therefore, the value of more than 32 digits can be saved with this bitwise notation, and there is no overflow problem when outputting.

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.