IOS development uses bitwise operators to record the coordinates of multiple buttons

Source: Internet
Author: User
Tags bitwise operators

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

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.