Skip to content

Instantly share code, notes, and snippets.

@sikanrong
Created August 2, 2018 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sikanrong/0d0a13e8b9b333a407b955b36cf65317 to your computer and use it in GitHub Desktop.
Save sikanrong/0d0a13e8b9b333a407b955b36cf65317 to your computer and use it in GitHub Desktop.
//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