Last active
March 8, 2017 12:09
-
-
Save bdw/92653ca6260e06167a1c703f95fd833b to your computer and use it in GitHub Desktop.
cross-platform first set bit
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
#include <stdio.h> | |
#include <stdlib.h> | |
typedef unsigned int MVMuint32; | |
#ifdef __GNUC__ | |
#define FFS(x) __builtin_ffs(x) | |
#elif defined(_MSC_VER) | |
static inline MVMuint32 FFS(MVMuint32 x) { | |
MVMuint32 i = 0; | |
if (_BitScanForward(&i, x) == 0) | |
return 0; | |
return i + 1; | |
} | |
#else | |
#error "Can't define Find First Set" | |
#endif | |
int main (int argc, char **argv) { | |
int v, f; | |
if (argc < 2) | |
return 0; | |
v = atoi(argv[1]); | |
f = FFS(v); | |
fprintf(stderr, "%d %d\n", v, f); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment