Transfer from: Http://www.tuicool.com/articles/niEVjy introduction
Bit operation is a unary and two-dollar operation of the bit-mode or binary number in the program design. On many ancient microprocessors, bit operations are slightly faster than addition and subtraction, and the bitwise operations are much faster than the multiplication algorithm. In modern architectures, this is not the case: bit operations are usually the same as the addition operation (still faster than multiplication). (From Wikipedia)
OC as an extension and superset of C, bit operations are naturally using the C operator. C provides 6 bit operator,$,|,^,~,<<,>>. This article does not intend to do the basic teaching of bit arithmetic, only introduces some scenarios which can be used in the development.
Increase computational speed
As mentioned in the previous paragraph, the operation speed of the bitwise operation is usually equal to the addition speed, but faster than the multiplication. Therefore, if our program has a performance requirement, we can use bit arithmetic to improve the operation speed. Like what:
- Multiplied by 2:n << 1;
- Divided by 2:n >> 1;
- Multiplied by 2 m-th square: n << m;
- Divided by 2 m-th square: n >> m;
- Judging parity: (n & 1) = = 1;
- Averaging: (A + B) >> 1;
- ......
There are many more speed-up algorithms based on the multiplication method, not listed here. It is important to note that you should only do this when you encounter performance bottlenecks, and the bottleneck is really calculated. Because the use of bit arithmetic is not conducive to the readability and maintainability of the program. (except scientific calculations)
Compressed space
Previously exposed to ACM cheese should be not unfamiliar with state compression, the purpose of state compression is to put a big data with limited memory space to represent. For example, a classic example in programming pearls: How do I sort a maximum of 10 million non-repeating 7-bit integers (phone numbers)? And can use more than 1MB of memory space.
The obvious general practice is to do a sort of out-of-disk operation. However, if you switch ideas, make full use of every bit in memory, plus no duplicate phone numbers, and no phone numbers starting with 0 and 1. We only need to use 10 million bits (about 1.2MB) to be able to tag all the data in memory in a collection, making it easy to sort the bits. This method drastically reduces IO time, resulting in significant performance gains.
ACM has a lot of if using bits to compress the space of the example, the dynamic planning of state compression, and so on, do not expand here, only tell the reader, full use of memory every bit, often can bring unexpected harvest. However, it should be noted that the state of compression and extraction, all need a certain amount of computation, sometimes blindly pursuit of state compression, but will reduce efficiency.
Represents data
Compare a classic application scenario, using a string of 24-bit 16-mechanism numbers to represent an RGB color (or 32-bit to denote ARGB). Because of the ps,web and all kinds of color picker, can quickly take out the RGB hex value, but Uicolor no corresponding method. Therefore, we can write the following Uicolor category, to quickly generate a uicolor with a rgbhex. (source code in Uicolor + CYHelper.h)
+ (uicolor *) Colorwithrgbhex: (UInt32) hex{ return [uicolor colorwithrgbhex:hex alpha:1.0f];} + (Uicolor *) Colorwithrgbhex: (UInt32) hex alpha: (cgfloat) alpha{int r = (hex >> 16) & 0XFF; int g = (hex >> 8) & 0xFF; int b = (hex) & 0XFF; return [uicolor colorwithred:r/255.0f green:g/ 255.0f blue:b/255.0f Alpha:alpha];}
Status and Options
typedef ns_options (Nsuinteger, uiviewanimationoptions) {uiviewanimationoptionlayoutsubviews =1 <<0,Uiviewanimationoptionallowuserinteraction =1 <<1,//TurnOnUserInteractionWhileAnimatingUiviewanimationoptionbeginfromcurrentstate =1 <<2,//StartAllViewsFromCurrentValueNotInitialValueUiviewanimationoptionrepeat =1 <<3,//RepeatAnimationIndefinitelyUiviewanimationoptionautoreverse =1 <<4,//IfRepeatRunAnimationBackandForthUiviewanimationoptionoverrideinheritedduration =1 <<5,//IgnoreNestedDurationUiviewanimationoptionoverrideinheritedcurve =1 <<6,//IgnoreNestedCurveUiviewanimationoptionallowanimatedcontent =1 <<7,//AnimateContents (AppliesToTransitionsonly)Uiviewanimationoptionshowhidetransitionviews =1 <<8,//Flipto/FromHiddenStateinsteadOfadding/removingUiviewanimationoptionoverrideinheritedoptions =1 <<9,//DoNotInheritAnyOptionsOrAnimationTypeUiviewanimationoptioncurveeaseinout =0 <<16,//DefaultUiviewanimationoptioncurveeasein =1 <<16,Uiviewanimationoptioncurveeaseout =2 <<16,Uiviewanimationoptioncurvelinear =3 <<16,Uiviewanimationoptiontransitionnone =0 <<20,//DefaultUiviewanimationoptiontransitionflipfromleft =1 <<20,Uiviewanimationoptiontransitionflipfromright =2 << 20, Uiviewanimationoptiontransitioncurlup = 3 << 20, uiviewanimationoptiontransitioncurldown = 4 << 20 , uiviewanimationoptiontransitioncrossdissolve = 5 << 20, uiviewanimationoptiontransitionflipfromtop = 6 << 20, Uiviewanimationoptiontransitionflipfrombottom = 7 << 20,} span class= "attribute" >ns_enum_available_ios (4_0);
We looked at Apple's enumeration variables in uiviewanimationoptions and used a nsuinteger to represent all the option required for uiviewanimation. One of the 0~9 10 is non-affected and can exist simultaneously option. The 16~19,20~24 uses 4 bits to represent mutually exclusive option.
Once this is defined, the assignment to the uiviewanimationoptions becomes particularly simple, using | Operator can obtain a result that assigns a value to the corresponding option bit. For example:
[UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseIn animations:{...} completion:{...}];
Extraction is also relatively simple, using the & operator and the >> operator, it is easy to determine whether a bit is set, and to extract some status bits, for example:
Uiviewanimationoptions option = Uiviewanimationoptionallowuserinteraction | Uiviewanimationoptionbeginfromcurrentstate | Uiviewanimationoptioncurveeasein | Uiviewanimationoptiontransitioncrossdissolve;if (option & uiviewanimationoptionallowuserinteraction) {NSLog (@ " Uiviewanimationoptionallowuserinteraction has been set ");} if (option & uiviewanimationoptionbeginfromcurrentstate) {NSLog (@ " Uiviewanimationoptionbeginfromcurrentstate has been set ");} UInt8 optioncurve = option >> & 0xf; if (Optioncurve = = 1) {NSLog (@ "Uiviewanimationoptioncurveeasein has been set");} UInt8 optiontransition = option >> & 0xf; if (optiontransition = = 5) {NSLog (@ "Uiviewanimationoptiontransitioncrossdissolve has been set");}
The most important thing to note here is that the setting of the state of the mutex must be especially careful, if you write:
16 & 0xf;NSLog(@"Sorry, it‘s not UIViewAnimationOptionCurveEaseInOut");NSLog(@"oops = %d, you got UIViewAnimationOptionCurveLinear", oops);
Contact me
- Write Mail: lancy1014#gmail.com
- Follow me on Weibo
- Fo my Github
- Write a comment message here
Lancy
9.27
Bitwise AND bitwise operations in the "Go" cocoa