9

Could the least significant bit in an integer technically be considered the isOdd flag?

Comments
  • 2
    yes you can, given the int is represented in memory as in C

    caveat:
    a negative number is a (positive number's one complement) - 1

    in practice:
    0000 1010 is 10

    1's complement of 10:
    1111 0101

    and finally -1;
    1111 0100 is -10

    so you end up with the adequate bit despite internal shenanigans

    edit: forgot that you -1 when you change signs
  • 3
    It pretty much is. Many of us are from an era where "&1" was by far the most common way to check for even / odd. None of this %2==1 nonsense 😁
  • 1
    Yes, it's pretty much by definition. An odd number is defined as 1 + 2k = 2^0 + k << 1, which can be read as "to construct an odd number, take any k, left shift it by 1, and set the LSB to 1 (the bits of k don't affect the LSB because of the shift)", so the LSB is always 1.
  • 1
    I need to remember this for the next code golf.
Add Comment