IsEven(int32_t) in O(1)
ceayo.neocities.org·8w·
Discuss: Hacker News

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...

Keyboard Shortcuts

Navigation
Next / previous item
j/k
Open post
oorEnter
Preview post
v
Post Actions
Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Recommendations
Add interest / feed
Enter
Not interested
x
Go to
Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Browse
gb
Search
/
General
Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help