I'll use uint64_t
here (which needs the <stdint.h>
include) so as not to require your assumption about the size of unsigned long
.
phuclv's idea of using -n
is clever, but can be made much simpler. As unsigned 64-bit integers, we have -n = 264-n, then (-n)/n = 264/n - 1, and we can simply add back the 1.
uint64_t divide_two_to_the_64(uint64_t n) {
return (-n)/n + 1;
}
The generated code is just what you would expect (gcc 8.3 on x86-64 via godbolt):
mov rax, rdi
xor edx, edx
neg rax
div rdi
add rax, 1
ret
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…