Created
August 2, 2018 11:23
-
-
Save sikanrong/0d0a13e8b9b333a407b955b36cf65317 to your computer and use it in GitHub Desktop.
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
//https://www.geeksforgeeks.org/check-if-a-given-number-is-sparse-or-not/ | |
/* | |
* check_sparse.c | |
* | |
* Created on: Aug 2, 2018 | |
* Author: sikanrong | |
*/ | |
#include <stdio.h>; | |
#include <stdint.h>; | |
int main(){ | |
uint8_t n; | |
printf("Enter a number to check: "); | |
scanf("%u", &n); | |
uint8_t lastBit; | |
uint8_t isSparse = 1; | |
while(n){ | |
lastBit = (n & 1); | |
n >>= 1; | |
if(lastBit && (n & 1)){ | |
isSparse = 0; | |
break; | |
} | |
} | |
if(isSparse){ | |
printf("is sparse."); | |
}else{ | |
printf("is not sparse."); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment