Created
December 1, 2015 01:25
-
-
Save antonio081014/e603e960d259c56739c9 to your computer and use it in GitHub Desktop.
This is an implementation of Basic Bit Manipulation Operation written in Objective-C.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (BOOL)getBitforNumber:(NSInteger)num atIndex:(NSUInteger)i | |
{ | |
return (num & (1 << i)) != 0; | |
} | |
- (NSInteger)setBitforNumber:(NSInteger)num atIndex:(NSUInteger)i | |
{ | |
return num | (1 << i); | |
} | |
- (NSInteger)clearBitforNumber:(NSInteger)num atIndex:(NSUInteger)i | |
{ | |
NSInteger mask = ~(1 << i); | |
return num & mask; | |
} | |
- (NSInteger)clearBitsforNumber:(NSInteger)num throughIndexI:(NSUInteger)i | |
{ | |
NSInteger mask = ~((1 << (i + 1)) - 1); | |
return num & mask; | |
} | |
- (NSInteger)updateBitforNumber:(NSInteger)num atIndex:(NSUInteger)i withValue:(NSUInteger)v | |
{ | |
NSInteger mask = ~(1 << i); | |
return (num & mask) | (v << i); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment