Use of bitwise operators

Source: Internet
Author: User

In this article, let's take a look at the objective-c bitwise operator . There are various operators in the objective-c language to handle the special positioning in numbers, as shown in the following table:

Symbol Operation
& Bitwise-AND
| Bitwise OR
^ Bitwise XOR OR
~ One-time negation
<< Shift left
>> Shift Right

All operators listed in the table are two-tuple operators except one-time negation operator (~), so two operands are required. Bitwise operators can handle any type of integer value, but cannot handle floating-point values.

1. Bitwise operators

When you perform and operate on two values, the binary representation of the two values is compared one bit at a time. The first value corresponds to the second value with a bit of 1 o'clock, the corresponding bit of the result is 1, and the other combinations are 0 in the result. If B1 and B2 represent the corresponding bits of the two operands, the following table (called the Truth Tables) shows the results of the execution and operation of B1 and B2 under all possible values of B1 and B2.

B1 B2 B1 & B2
——————————
0 0 0
0 1 0
1 0 0
1 1 1

For example, if W1 and W2 are defined as short int, W1 equals 16, and W2 equals 16 in 0c, the following C statement assigns the value 0x04 to W3.

W3 = W1 & W2;

This procedure can be seen more clearly after W1, W2, and W3 are represented as binary, assuming that the size of the short int processed is 16 bits.

W1 0000 0000 0001 0101 0x15
W2 0000 0000 0000 1100 & 0X0C
————————————————————
W3 0000 0000 0000 0100 0x04

Bitwise AND operations are often used for shielding operations. That is, this operator can easily set a specific bit of a data item to 0. For example, the statement

W3 = W1 & 3;

Assigns the W1 and the constant 3 bitwise and the resulting value to the W3. It does this by setting all the bits in the W3 (not the rightmost two bits) to 0 and preserving the leftmost two bits in the W1.

As with all the two-dollar operators used in Objective-c, the two-bit operator can be used as an assignment operator by adding an equal sign. So the statement

Word &= 15;

With the following statement

Word = word & 15;

Perform the same function.

In addition, it can set the whole word to 0, except for the rightmost four digits.

2. Bitwise OR operator

When a bitwise OR operation is performed on a two value in objective-c, a binary representation of two values is compared bit by digit. At this point, as long as the first value or the corresponding bit of the second value is 1. Then the corresponding bit of the result is 1. The truth table for the bitwise OR operator is shown below.

B1 B2 B1 | B2
———————————
0 0 0
0 1 1
1 0 1
1 1 1

So, if W1 is short int, equals 16 binary, W2 is also short int, equals 16 binary 6a, then bitwise OR will get hexadecimal 7b for W1 and VV2, as follows:

W1 0000 0000 0001 1001 0x19
W2 0000 0000 0110 1010 | 0x6a
————————————————————
0000 0000 0111 1011 0x7b

A bitwise OR operation is often referred to as a bitwise OR, which is used to set a specific bit of a word to 1. For example, the following statement sets the rightmost three bits of the W1 to 1, regardless of the state before those bit operations.

W1 = W1 | 07;

Of course, you can use a special bin-value operator in a statement, as shown in the following statement:

W1 |= 07;

We will provide a program example later that shows how to use a bitwise OR operator.

3, Bitwise XOR OR operator

The bitwise XOR operator, commonly referred to as the XOR operator, adheres to the following rules: If any one bit is 1, but not both 1, then the corresponding bit of the result will be 1, otherwise 0. The truth table for this operator is as follows
is shown below:

B1 B2 B1 ^ b2
————————————
0 0 0
0 1 1
1 0 1
1 1 0

If W1 and W2 are equal to 16 binary 5e and D6 respectively, then the result of W1 and W2 performing an XOR operation is the hexadecimal value E8, as follows:

W1 0000 0000 0101 1110 0X5E
W2 0000 0000 1011 0110 ^ 0xd6
——————————————————————
0000 0000 1110 0xe8

In this article, first of all, for the objective-c bit operators We continue to discuss the next, the main discussion of the objective-c bitwise operator in the first negation, left shift operation, right shift operation, see.

1, one-time negation operation

One-time negation operator is a unary operator that acts only as a bit "flip" to the operand. Flips the number of operands to 1 for each bit to 0, and flips each 0 bit to 1. The truth table is provided here only to preserve the integrity of the content.

B1 ~B1
——————
0 1
1 0

If W1 is short int, 16 bits long, equals hexadecimal value a52f, then performing a negation operation on the value will get the hexadecimal value 5ab0:

W1 1010 0101 0010 1111 0xa52f
~W1 0101 1010 1101 0000 0x5ab0

If you do not know the exact bit size of the numeric value in the operation, it is useful to have the negation operator once, and use it so that the program does not depend on the specific size of the integer data type. For example, to set the lowest bit of W1 of type int to 0, an int value with w1 that all bits are 1, but the rightmost bit is 0, is evaluated with the operation. So a C statement like this can work correctly on a machine with 32-bit integers.

W1 &= 0xFFFFFFFE;

If you use

W1 &=;

Replace the above statement, then the W1 on any machine will be with the correct value and operation.

This is because this statement will reverse 1, and then add enough 1 to the left to meet the size of the int (on a 32-bit machine, 1 is added to the 31 bits on the left).

Now, show an example of a practical program that illustrates the purpose of the various bitwise operators

123456789101112131415 // Bitwise operators illustrated#import <foundation foundation.h="">intmain (intargc, char*argv[]){NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];unsigned intw1 = 0xA0A0A0A0, w2 = 0xFFFF0000,w3 = 0x00007777;NSLog (@”%x %x %x”, w1 & w2, w1 | w2, w1 ^ w2);NSLog (@”%x %x %x”, ~w1, ~w2, ~w3);NSLog (@”%x %x %x”, w1 ^ w1, w1 & ~w2, w1 | w2 | w3);NSLog (@”%x %x”, w1 | w2 & w3, w1 | w2 & ~w3);NSLog (@”%x %x”, ~(~w1 & ~w2), ~(~w1 | ~w2));[pool drain];return0;}</foundation>

Result output:

a0a00000 ffffa0a0 5f5fa0a0
5f5f5f5f FFFF ffff8888
0 a0a0 Fffff7f7
A0A0A0A0 ffffa0a0
FFFFA0A0 a0a00000

Make a calculation of each operation in the code, and be sure you understand how the results are obtained.

In the fourth NSLog call, it is important to note that the bitwise AND operator precedence is higher than the bitwise OR operator because it actually affects the final result value of the expression.

The fifth NSLog call shows the DeMorgan rule: ~ (~a & ~b) equals a | b,~ (~a | ~b) equals A & B.

2. Shift operator to the left

When you perform a left shift operation on a value, the bits contained in the value are moved to the left by literal means. Associated with the operation is the number of positions (or bits) to which the value is to be moved. Bits that exceed the high position of the data item are lost, and the value that is moved from the low is always 0. So, if W1 equals 3, then the expression

W1 = W1 << 1;

Can also be expressed as

W1 <<= 1;

The result is a 3 shift to the left, so that 6 of the resulting value is assigned to W1.

W1 ... 0000 0011 0x03
W1 << 1 ... 0000 0110 0x06

3, right shift operator

As the name implies, the right shift operator (>>) moves the bits of the value to the right. Bits that are removed from the low value are lost. Shifting the unsigned value to the right is always the left (or high) shift 0. For signed values, moving the left in 1 or 0 depends on the symbol of the moved number, and also depends on how the operation is implemented on the computer. If the sign bit is 0 (indicating that the value is positive), no matter which machine will move the person 0. However, if the sign bit is 1, the person will be moved 1 on some computers and 0 on the other computer. Operators of the previous type are often referred to as arithmetic right shifts, which are often referred to as logical right shifts.

If W1 is unsigned int, it is represented with 32 bits and it equals + six f777ee22, then use the statement

W1 >>= 1;

After moving the W1 to the right one, the W1 equals 16 binary 7bbbf711, as follows:

W1 1111 0111 0111 0111 1110 1110 0010 0010 0xf777ee22
W1 >> 1 0111 1011 1011 1011 1111 0111 0001 0001 0x7bbbf711

If you declare W1 as a (signed) short int, you get the same result on some computers, and on other computers, if you move the operation as an arithmetic right, the result will be FBBBF711.

It should be noted that if you attempt to shift a value to the left or right with the number of digits greater than or equal to the data item, the objective-c language does not produce the specified result. So, for example, when a computer uses 32 bits to represent integers, moving an integer 32 or more bits to the left or right does not produce the prescribed results on the computer. It is also noted that if you use negative numbers to shift values, the results will be equally undefined.

Well, through the introduction of these two articles, we should have a certain understanding of the objective-c bit operators.

Use of bitwise operators

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.