Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <time.h>
- #include <stdio.h>
- #include <stdint.h>
- int Log2FloorBASE(uint32_t n) {
- if (n == 0)
- return -1;
- int log = 0;
- uint32_t value = n;
- for (int i = 4; i >= 0; --i) {
- int shift = (1 << i);
- uint32_t x = value >> shift;
- if (x != 0) {
- value = x;
- log += shift;
- }
- }
- return log;
- }
- int Log2FloorNEW(uint64_t n) {
- if (!n) return -1;
- int log = 0;
- for (int i = 5; i >= 0; ++i) {
- int shift = 1 << i;
- if (n >> shift) {
- log += shift;
- if (!(n >>= shift)) break;
- }
- }
- return log;
- }
- int main() {
- time_t t = time(NULL);
- printf("base: %d\n", Log2FloorBASE(128));
- printf(" took: %ld seconds\n", time(NULL) - t);
- t = time(NULL);
- printf("new: %d\n", Log2FloorNEW(128));
- printf(" took: %ld seconds\n", time(NULL) - t);
- return 0;
- }
- $ g++ -o test test.cc && ./test
- base: 7
- took: 0 seconds
- new: 7
- took: 9 seconds
Advertisement
Add Comment
Please, Sign In to add comment