What is a bit operation
Binary is a number system widely used in computing technology. Binary data are numbers represented by 0 and 1 digits. All numbers in a program are stored in binary form in computer memory, and operating directly on binary data in memory is called bitwise operations.
The operation mode of bit operation
There are six basic bit operations, as follows:
- Bitwise and
- Bitwise or
- The bitwise exclusive or
- According to the not
- Shift to the left
- Moves to the right
Let’s talk about them separately.
Bitwise and
Operation symbol: &
Operation rule: If both digits of the same bit are 1, it is 1. If one of them is not 1, it’s 0
Example operation:
0 0 1 0 1 1 1 0 46 1 0 0 1 1 1 0 1 157 ——————————————— 0 0 0 0 1 1 0 0 1 1 0 0 12Copy the code
Bitwise or
The operational sign: |
Operation rule: As long as one of the same bits is 1, it is 1
Example operation:
0 0 1 0 1 1 1 0 46 1 0 0 1 1 1 1 0 1 157 ——————————————— 1 0 1 1 1 1 1 1 1 1 1 191Copy the code
The bitwise exclusive or
Operation symbol: ^
Operation rule: 1 if the same bit is different, 0 if the same bit is the same
Example operation:
0 0 1 0 1 1 1 0 46 1 0 0 1 1 1 0 1 157 ——————————————— 1 0 1 1 0 1 1 0 0 1 1 179Copy the code
According to the not
Operation symbol: ~
Operation rules: ~ is the unary operator used to invert a binary number bit by bit, changing 0 to 1 and 1 to 0
Example operation:
0 0 1 0 1 1 1 0 46 ——————————————— 1 1 0 1 0 1 0 0 1 225Copy the code
Shift to the left
Operation symbol: <<
Rule of operation: a << b indicates that a is converted to binary by b bits to the left (followed by b zeros). That is, each binary all left shift a number of bits, high discard, low fill 0
Example operation:
0011 << 1 => 0110
Copy the code
Moves to the right
Operation symbol: >>
Operation rules: shift all binary bits to the right, add 0 to unsigned numbers, add 0 to high, add signed numbers, different compiler processing method, some complement sign bits (arithmetic right shift), some complement 0 (logical right shift)
Example operation:
0110 >> 1 => 0011
Copy the code
Bit operations are used in daily development
Swap the values of two variables
If you have two numbers A and b, how do you swap the values of a and B without using a third variable?
a^=b
即a=(a^b)
;b^=a
即b=b^(a^b)
, since ^ operation satisfies the commutative law,b^(a^b)=b^b^a
. B is assigned because a number is 0 and any number is equal to 0a
The value of the.a^=b
isa=a^b
, due to the previous two stepsa=(a^b)
B =a, soA = a ^ b is a = (a ^ b) ^ a
. Therefore,a
It’s going to be assigned the value of B.
The code is as follows:
int a = 10;
int b = 20;
if(a ! = b) { a ^= b; b ^= a; a ^= b; }NSLog(@"a: %@ -- b: %@", @(a), @(b));
// The output is
// a: 20 -- b: 10
Copy the code
Judge parity
We can decide whether the last digit is 0 or 1, 0 is even, 1 is odd. So you can use ((a & 1) == 0) instead of (a % 2 == 0) to determine whether a number is odd or even.
for (int i = 0; i < 5; I ++) {NSLog(@" I: %@, is %@\n", @(I), (I & 1)? @" odd ": @" even "); } / / output is / / I: 0, is an even number / / I: 1, is odd / / I: 2, is an even number / / I: 3, is odd / / I: 4, is evenCopy the code
Transformation of symbols
Transformation notation is simply a positive to a negative, and a negative to a positive. The sign transformation can be done by taking the inverse plus one of the numbers.
int a = -10; NSLog(@"a ": %@", @(~a + 1)); // The output is // a: 10Copy the code
Displacement of the enumeration
In iOS, we have two enum types: NS_OPTION and NS_ENUM. The essence of both of them is the same, the difference is whether you can choose more than one.
NS_OPTION
: Multiple enumerations using bitwise or (|
Adding indicates multiple operations. Different bits of displacement result in different values, that is, the enumeration value represented by the value is different, and the simultaneous use of multiple enumerations is still unique.NS_ENUM
: Cannot be multiple, only mutually exclusive.
What is the displacement enumeration
Displacement enumeration is a special enumeration. The difference between it and ordinary enumeration is whether it can be selected more than one time.
Displacement enumeration of multiple enumerated values at the same time using the bitwise or (|) said additive for multiple operation. Different bits of displacement result in different values, that is, the enumeration value represented by the value is different, and the simultaneous use of multiple enumerations is still unique.
For example,
Create a custom view, AddLabelView, to create a property that defines where to add a label.
// AddLabelView
typedef NS_OPTIONS(NSUInteger, AddLabelPosition) {
AddLabelTopLeft = 1 << 0,
AddLabelTopRight = 1 << 1,
AddLabelBottomLeft = 1 << 2,
AddLabelBottomRight = 1 << 3,
AddLabelAllCorners = 1 << 4
};
@interface AddLabelView : UIView
@property (nonatomic.assign) AddLabelPosition addLabelPosition;
@end
@implementation AddLabelView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor greenColor];
}
return self;
}
- (void)setAddLabelPosition:(AddLabelPosition)addLabelPosition {
if (addLabelPosition & AddLabelTopLeft) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0.self.bounds.size.width * 0.5.self.bounds.size.height * 0.5)];
label.text = @ "1";
label.textAlignment = NSTextAlignmentCenter;
[self addSubview:label];
}
if (addLabelPosition & AddLabelTopRight) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.bounds.size.width * 0.5.0.self.bounds.size.width * 0.5.self.bounds.size.height * 0.5)];
label.text = @ "2";
label.textAlignment = NSTextAlignmentCenter;
[self addSubview:label];
}
if (addLabelPosition & AddLabelBottomLeft) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.self.bounds.size.width * 0.5.self.bounds.size.width * 0.5.self.bounds.size.height * 0.5)];
label.text = @ "3";
label.textAlignment = NSTextAlignmentCenter;
[self addSubview:label];
}
if (addLabelPosition & AddLabelBottomRight) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.bounds.size.width * 0.5.self.bounds.size.width * 0.5.self.bounds.size.width * 0.5.self.bounds.size.height * 0.5)];
label.text = @ "4";
label.textAlignment = NSTextAlignmentCenter;
[self addSubview:label];
}
if (addLabelPosition & AddLabelAllCorners) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0.self.bounds.size.width, self.bounds.size.height)];
label.text = 5 "@";
label.textAlignment = NSTextAlignmentCenter;
[selfaddSubview:label]; }}@end
Copy the code
Create a custom view that controls where we display a label by adding LabelPosition.
AddLabelView *view = [[AddLabelView alloc] initWithFrame:CGRectMake(100.100.200.200)];
view.addLabelPosition = AddLabelTopRight | AddLabelBottomRight | AddLabelBottomLeft;
[self.view addSubview:view];
Copy the code
From the above code, you should already understand the concept of multiple selection of displacement enumeration, if you really don’t understand, welcome private message.
The last
Hopefully, this article’s introduction to bit arithmetic has given you some insight into how you can use it to solve problems more easily in your daily work.
If you think this article is good, please like 👍, save 🌟, share 👊, and watch 👀.