Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- void printBits(short value) {
- short i = sizeof(value)*8 - 1;
- for (; i >= 0; i--) {
- printf("%i", (value >> i) & 1);
- if (i % 4 == 0)
- printf(" ");
- }
- printf("\n");
- }
- short setBit(short value, short position) {
- return (value | (1 << position));
- }
- short clearBit(short value, short position) {
- return ~(1 << position) & value;
- }
- short flipBit(short value, short position) {
- return (1 << position) ^ value;
- }
- short getBit(short value, short position) {
- return ((1 << position) & value) >> position;
- }
- int main() {
- short value = 15;
- printBits(value);
- value = clearBit(value, 1);
- printBits(value);
- value = setBit(value, 1);
- printBits(value);
- value = flipBit(value, 1);
- printBits(value);
- printf("%i", getBit(value, 1));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement