Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- void setBit(int* mask, int n)
- {
- *mask |= (1 << n);
- }
- void eraseBit(int* mask, int n)
- {
- *mask &= ~(1 << n);
- }
- int isSet(int mask, int n)
- {
- return mask & (1 << n);
- }
- int onesCount(int mask)
- {
- int count = 0;
- for (int i = 0; i < sizeof(int) * 8; i++)
- {
- if (isSet(mask, i))
- {
- count++;
- }
- }
- return count;
- }
- int zerosCount(int mask)
- {
- return sizeof(int) * 8 - onesCount(mask);
- }
- int main()
- {
- int prisustviq = 0;
- setBit(&prisustviq, 1);
- setBit(&prisustviq, 3);
- setBit(&prisustviq, 0);
- //eraseBit(&prisustviq, 3);
- printf("%d\n", onesCount(prisustviq));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement