#include <stdio.h>
int count_consecutive_ones(int in) {
int count = 0;
while (in) {
in = (in & (in << 1));
count ++;
}
return count;
}
int count_consecutive_zeros(int in) {
return count_consecutive_ones(~in);
}
int main(void) {
printf("%d has %d consecutive 1's\n", 0, count_consecutive_ones
(0)); printf("%d has %d consecutive 0's\n", 0, count_consecutive_zeros
(0));
/* 00000000 11110000 00000000 00000000 -> If it is 0 then length will be 20 */
printf("%x has %d consecutive 0's\n", 0x00F00000, count_consecutive_zeros
(0x00F00000));
/* 11111111 11110000 11110111 11111111 -> If it is 1 then length will be 12 */
printf("%x has %d consecutive 1's\n", 0xFFF0F7FF, count_consecutive_ones
(0xFFF0F7FF));
}
I2luY2x1ZGUgPHN0ZGlvLmg+CgppbnQgY291bnRfY29uc2VjdXRpdmVfb25lcyhpbnQgaW4pIHsKICBpbnQgY291bnQgPSAwOwoKICB3aGlsZSAoaW4pIHsKICAgIGluID0gKGluICYgKGluIDw8IDEpKTsKICAgIGNvdW50ICsrOwogIH0KICByZXR1cm4gY291bnQ7Cn0KCmludCBjb3VudF9jb25zZWN1dGl2ZV96ZXJvcyhpbnQgaW4pIHsKICByZXR1cm4gY291bnRfY29uc2VjdXRpdmVfb25lcyh+aW4pOwp9IAoKaW50IG1haW4odm9pZCkgewoKICBwcmludGYoIiVkIGhhcyAlZCBjb25zZWN1dGl2ZSAxJ3NcbiIsIDAsIGNvdW50X2NvbnNlY3V0aXZlX29uZXMoMCkpOwogIHByaW50ZigiJWQgaGFzICVkIGNvbnNlY3V0aXZlIDAnc1xuIiwgMCwgY291bnRfY29uc2VjdXRpdmVfemVyb3MoMCkpOwoKICAvKiAwMDAwMDAwMCAxMTExMDAwMCAwMDAwMDAwMCAwMDAwMDAwMCAtPiBJZiBpdCBpcyAwIHRoZW4gbGVuZ3RoIHdpbGwgYmUgMjAgKi8KICBwcmludGYoIiV4IGhhcyAlZCBjb25zZWN1dGl2ZSAwJ3NcbiIsIDB4MDBGMDAwMDAsIGNvdW50X2NvbnNlY3V0aXZlX3plcm9zKDB4MDBGMDAwMDApKTsKCiAgLyogMTExMTExMTEgMTExMTAwMDAgMTExMTAxMTEgMTExMTExMTEgLT4gSWYgaXQgaXMgMSB0aGVuIGxlbmd0aCB3aWxsIGJlIDEyICovCiAgcHJpbnRmKCIleCBoYXMgJWQgY29uc2VjdXRpdmUgMSdzXG4iLCAweEZGRjBGN0ZGLCBjb3VudF9jb25zZWN1dGl2ZV9vbmVzKDB4RkZGMEY3RkYpKTsKCn0=