This article reprinted to http://my.oschina.net/sunqichao/blog?disp=2&catalog=0&sort=time&p=2
In addition to the operators described in the basic operators, Swift also has many sophisticated advanced operators, including bitwise operators and shift operations in C and Objective-c.
Unlike numerical calculations in C, Swift's numerical calculations are not overflow by default. The overflow behavior is captured and reported as an error. Did you do it on purpose? Well, you can use another set of default allowable overflow numeric operators, such as overflow plus &+, that Swift has prepared for you. All operators that allow overflow are started with &.
Custom structures, classes, and enumerations, can you use standard operators to define operations? Of course! In Swift, you can customize the operation of operators for all types that you create.
Customizable operators are not limited to pre-defined operators, and customize the central, pre-set, post-and assignment operators of personalities, and of course, precedence and binding. Implementations of these operators can use pre-defined operators, or they can use previously customized operators.
Bitwise operators
Bit operators are commonly used in the underlying development of image processing and creation of device drivers, which can be used to manipulate the bits of the original data in a data structure independently. It is also very effective to encode and decode raw data using bit operators when communicating with a custom protocol.
Swift supports all C-language bitwise operators as follows:
Bitwise inverse operator
Bitwise inverse Operator ~ is reversed for each bit of an operand.
This operator is pre-placed, so please write the operand without any whitespace.
Let initialbits:uint8 = 0b00001111 let invertedbits = ~initialbits//equals 0b11110000
UInt8 is a 8-bit, no-character integer that can store any number between 0~255. This example initializes an integer to a binary value of 00001111 (the first 4 bits are 0, the last 4 bits are 1), and its decimal value is 15.
Use bitwise negation to initialbits operation, then assign to invertedbits the new constant. The value of this new constant is equal to the initialbits that all bits are reversed, that is, 1 becomes 0, 0 becomes 1, becomes 11110000, and the decimal value is 240.
Bitwise-AND operator
The bitwise-AND operator operates on two numbers, and then returns a new number, where each bit of the number requires a number of two inputs for the same 1 o'clock to 1.
The following code, both Firstsixbits and lastsixbits, has a median of 4 bits of 1. After the bitwise AND operation of the two, we get 00111100, which is the decimal 60.
Let firstsixbits:uint8 = 0b11111100 let lastsixbits:uint8 = 0b00111111 let middlefourbits = firstsixbits & LastSixB its//equals 00111100
Bitwise OR operation
Bitwise OR operator | Compares two numbers and returns a new number, each of which is set to a condition of 1 that is not 0 (that is, either 1, or 1) for each bit of the two input number.
In the following code, Somebits and Morebits have 1 on different bits. The bitwise or run result is 11111110, which is the decimal 254.
Let somebits:uint8 = 0b10110010 let morebits:uint8 = 0b01011110 let combinedbits = Somebits | Morebits//Equals 11111110
Bitwise XOR OR operator
The bitwise XOR operator ^ compares two numbers and returns a number that is set to 1 for each bit of the two input number, if the same is set to 0.
The following code, Firstbits and Otherbits have a 1 different from the other number. So the bitwise XOR result is that it has these positions at 1, and the others are set to 0.
Let firstbits:uint8 = 0b00010100 let otherbits:uint8 = 0b00000101 let outputbits = firstbits ^ otherbits//Equals 00010001
Bitwise left SHIFT/Right shift operator
The left-shift operator << and right-shift operator >> moves all bits of a number to the left or right by the rules defined below to specify the number of digits.
The effect of bitwise left SHIFT and bitwise right SHIFT is quite an integer multiplied by or in addition to an integer with a factor of 2. Moving one integer to the left is equivalent to multiplying this number by 2, and moving one to the right is in addition to 2.
Shift operation with no-character integer
The effect of a shift to a non-integral type is as follows:
The already existing bits move left or right by the specified number of digits. The number of bits removed from the integer storage boundary is discarded directly, and the left margin is filled with Zezalai. This method is called logical shift.
The following shows the 11111111 << 1 (11111111 to the left 1 bits), and 11111111 >> 1 (11111111 to the right 1 bits). The blue is shifted, the gray is discarded, and the orange 0 is filled in.
Let Shiftbits:uint8 = 4//binary 00000100 shiftbits << 1//00001000 shiftbits << 2 00010000 shiftbits << 5//10000000 shiftbits << 6//00000000 Shiftbits >> 2 00000001
You can use the shift operation to encode and decode other data types.
Let Pink:uint32 = 0xcc6699 let redcomponent = (Pink & 0xFF0000) >>//redcomponent is 0xCC, i.e. 204 let Gree Ncomponent = (Pink & 0x00FF00) >> 8//greencomponent is 0x66, i.e. 102 let Bluecomponent = pink & 0x0000FF Bluecomponent is 0x99, that is, 153.
This example uses a UInt32 named Pink constant to store the pink color values in the cascading style sheet CSS, which is represented by the CSS color #cc6699 in hexadecimal 0xcc6699 in Swift. The Red (CC), Green (66), and Blue (99) Three parts can be resolved from this color value by using the bitwise AND (&) and bitwise right shifts.
The red part can be obtained by bitwise AND & operation on 0xcc6699 and 0xff0000. 0 of 0xff0000 covers the second and third bytes of OxCC6699, so 6699 is ignored, leaving only 0xcc0000.
Then, press 16 bits to the right, i.e. >> 16. Every two characters in hexadecimal is 8 bits, so moving the 16-bit result is turning 0xcc0000 into 0x0000cc. This is equal to 0xCC and is 204 of the decimal.
Similarly, the green part comes from the bitwise operation of 0xcc6699 and 0x00ff00 to get 0x006600. Then move 8 to the right and get 0x66, which is the decimal 102.
Finally, the blue part of the 0xcc6699 and 0X0000FF bitwise AND operation, get 0x000099, do not need to shift to the right, so the result is 0x99, that is, the decimal 153.
Shift operation with a character integral type
The shift operation of the integral type is much more complicated, because the sign is also expressed in bits. (The example here is 8-bit, but its principle is universal.) )
A character integer expresses whether the integer is positive or negative by a 1th bit (called the sign bit). 0 represents a positive number, and 1 represents a negative number.
The rest of the bits (called value bits) store the value in fact. There are regular integers and no regular integers in the computer where the stored result is the same, down we look at +4 internal binary structure.
The sign bit is 0 and represents a positive number, and the actual value of the 7 bit binary representation is exactly 4.
Negative numbers are different from positive numbers. Negative numbers store 2 of the N-square minus its absolute value, and n is the number of digits of the value bit. A 8-bit number has 7 numeric digits, so it is 2 7-square, or 128.
Let's see-4 stored binary structure.
Now the sign bit is 1, which represents a negative number, and the binary value to be expressed by 7 value bits is 124, or 128-4.
A negative number is encoded as a binary complement representation. This representation looks strange, but it has several advantages.
First, the standard binary addition of all 8 bits (including symbols) is required to complete the operation of-1 +-4, ignoring any information generated by the addition process for more than 8 bits expressed.
Second, because of the use of twos complement notation, we can shift the negative number to the right shift to the left, as well as the left 1-bit when multiplying by 2, the right shift 1 bits in addition to 2. To achieve this, there is a special requirement for the right shift of the character integral type:
When the bitwise right shifts for a character integer, the sign bit (positive number is 0 and negative number 1) fills the blank bit.
This ensures that the symbol for the character integer is not changed during the right shift. This is called arithmetic shift.
Due to the special storage of positive and negative numbers, the right shift makes it close to 0. The symbol remains unchanged during the shift and negative numbers are negative in the process of approaching 0.
Overflow operator
By default, when you assign an integer constant or variable to a large number that it cannot host, Swift will not let you do so, and it will give an error. In this way, it is safe to operate too large or too small a number.
For example, the integer range that the Int16 integer can carry is 32768 to 32767, and if it is assigned more than this range, an error is given:
var potentialoverflow = Int16.max//Potentialoverflow equals 32767, which is the largest integer Int16 can host Potentialoverflow + = 1//Oh, something went wrong.
Error handling of large or too small values makes your numerical boundary conditions more flexible.
Of course, you intentionally truncate the valid bits at overflow time, and you can use overflow operations instead of error handling. Swfit provides 5 overflow operators at the beginning of the & symbol for integer calculations.
overflow addition &+
Overflow subtraction &-
Overflow multiplication &*
Overflow Division &/
Overflow to seek residual &%
Overflow on a value
The following example uses an overflow addition &+ to dissect the overflow of a no-character integer
var willoverflow = Uint8.max//Willoverflow equals UInt8 maximum integer 255 willoverflow = willoverflow &+ 1//This time willoverflow equals 0
Willoverflow the maximum value 255 (binary 11111111) that can be carried by the Int8, and then use the &+ plus 1. Then UInt8 can not express the new value of the binary, it will lead to this new value overflow, we could see. After overflow, the new value in the UInt8 of the load range of the portion is 00000000, that is, 0.
The bottom overflow of the value
Values can also be crossed out because they are too small. As an example:
The minimum value for the UInt8 is 0 (binary is 00000000). Using &-for overflow minus 1, you get the binary 11111111, which is the decimal 255.
The SWIFT code is this:
var willunderflow = uint8.min//Willunderflow equals UInt8 minimum value 0 willunderflow = Willunderflow &-1//At this time Willunderflow equals 255
There is a similar overflow in the integer type, and all the subtraction of the character integer is binary subtraction of the binary number including the sign bit, which is mentioned in the "bitwise LEFT/Right Shift operator" section. The smallest character integer is-128, which is the binary 10000000. By subtracting 1 from overflow subtraction, it becomes 01111111, the largest integer 127 that UInt8 can carry.
Take a look at the SWIFT code:
var signedunderflow = int8.min//Signedunderflow equals the smallest signed integer -128 signedunderflow = signedunderflow &-1//Now Signedun Derflow equals 127.
except 0 overflow
A number in addition to 0 i/0, or 0 for the remainder i% 0, will produce an error.
Let x = 1 let y = x/0
Use their corresponding overflow version of the operator &/and &% to get a value of 0 for 0 operations.
Let x = 1 let y = x &/0//y equals 0
Swift Advanced Operators