We have all learned bit operations. In our work, using good bit operations can greatly improve program efficiency. Here is a simple example of my application:
-(Void) buttonClick :( id) sender {
UIButton * button = (UIButton *) sender;
Int tag = button. tag;
Int x = (tag> 8 )&
0xF;
Int y = tag & 0x0F;
NSLog (@ "(% d, % d)", x, y );
}
-(Void) initMap {
For (int x = 0; x <10; x ++ ){
For (int y = 0; y <10; y ++ ){
UIButton * backgroundButton = [UIButton
ButtonWithType: UIButtonTypeRoundedRect];
BackgroundButton. frame =
CGRectMake (15 + y * 29,
15 + x * 29, gridWidth,
GridHeight );
BackgroundButton. tag = (x <8) | y;
// Convert x to binary, and then shift it to the left, and then take the same value as y.
[BackgroundButton addTarget: self
Action: @ selector (buttonClick :)
ForControlEvents: UIControlEventTouchUpInside];
[Self. view
AddSubview: backgroundButton];
}
}
}
The bitwise operator C language provides six bitwise operators: & bitwise AND, | bitwise AND, ^
By bit or ,~ Inverse: <left shift. We will not describe the related applications one by one. There are many online applications.
> Right shift