IsEven(int32_t) in O(1)
ceayo.neocities.org·4d·
Discuss: Hacker News
🦀Rust
Preview
Report Post

After reading this article, I decided checking whether a 32 bit signed integer is even can be done more quickly. An approach to evenness detection based on if statements takes more or less time depending on the number that is placed as input. Therefore, Andreas’ implementation is O(n). I propose a more powerful, more optimized approach, which is O(1).

But how?

My approach is based on building a table. For every int32_t, I write down whether it is even or not. My result looks something like this:

$ xxd TRUTH32E.BIN | head
00000000: 0001 0001 0001 0001 0001 0001 0001 0001  ................
00000010: 0001 0001 0001 0001 0001 0001 0001 0001  ................
00000020: 0001 0001...

Similar Posts

Loading similar posts...