Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int segments(unsigned n) {
- int r = 1, prevBit = n & 1, bit;
- for (int i = 1; i < sizeof(n) * 8 - 1; i++) {
- bit = (n >> i) & 1;
- r += (bit != prevBit);
- prevBit = bit;
- }
- return r;
- }
- int main() {
- unsigned n;
- printf("n="); scanf("%u", &n);
- printf("segments: %d\n", segments(n));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement