News for hackers. Stuff that matters.
Re:Low Level Bit Hacks You Absolutely Must Know by craftyguy on Friday May 11, 2018 @01:02AM (#17045066)
I would think a "if (x % 2 == 1) //odd" would be within the realm of most CS applicants.
Re:Low Level Bit Hacks You Absolutely Must Know by kazinator on Friday May 11, 2018 @01:05AM (#17045074)
If x is a signed type, and negative, are you sure the modulus is positive 1?

Since ISO C99, C's division operator has been required to truncate toward zero. The % operator harmonizes with that and so it yields negative remainders south of zero.

In C90, it was implementation-defined, IIRC.

You really just want to test for zero/nonzero, rather than x % 2 == 1.

Re:Low Level Bit Hacks You Absolutely Must Know by craftyguy on Friday May 11, 2018 @01:14AM (#17045108)
Good point. Interestingly enough, Python seems to handle this appropriately:

    >>> -4%2
    0
    >>> -8%2
    0
    >>> -9%2
    1
Re:Low Level Bit Hacks You Absolutely Must Know by panic on Friday May 11, 2018 @02:46AM (#17045404)
Yeah, Python is one of the few languages which actually defines sane behavior for integer division and modulus rather than leaving it up to the hardware. I wish more languages would follow its example!
Re:Low Level Bit Hacks You Absolutely Must Know by triska on Friday May 11, 2018 @05:27AM (#17045936)
Prolog is also like this:

    ?- x is -4 mod 2.
    X = 0.
    ?- X is -8 mod 2.
    X = 0. 
    ?- X is -9 mod 2.
    X = 1.
Using declarative integer arithmetic, we can even express very generally that a number X is a multiple of 2:

    ?- X #= 2*_.
This is true iff there is any integer (indicated by an anonymous variable _) that, multiplied by 2, yields X. It works for positive and negative integers.

For example:

    ?- -4 #= 2*_.
    true.
    ?- -9 #= 2*_.
    false.
A very nice reference on the general topic of bitwise operations is Donald Knuth's Bitwise Tricks and Techniques in Volume 4 of The Art of Computer Programming.
Re:Low Level Bit Hacks You Absolutely Must Know by kazinator on Friday May 11, 2018 @10:43AM (#17047619)
ISO C doesn't leave it up to the hardware. Integer division is required to truncate toward zero; the remainder harmonizes with that. (If someone designs a new HLL which leaves some arithmetic to the hardware that C doesn't, that is indeed silly.)

The sane language here Common Lisp: it does it in all the ways rather than picking one approach with some hand-waving justification about why it's the good choice.

It has truncate, round, ceiling and floor functions that all return a quotient and remainder.

There are also mod and rem functions that calculate just the remainder from a floor and truncate, respectively.

Re:Low Level Bit Hacks You Absolutely Must Know by bakery2k on Friday May 11, 2018 @09:40AM (#17047207)
Guido has written a blog post explaining "Why Python's Integer Division Floors" [1].

Python also uses floor division, and the corresponding "sign-of-modulo matches sign-of-divisor", for floating-point numbers. The blog post claims that this can be problematic in certain cases (compared to C's "sign-of-modulo matches sign-of-dividend"), but does not give any concrete examples.

[1] http://python-history.blogspot.com.au/2010/08/why-pythons-in...

Re:Low Level Bit Hacks You Absolutely Must Know by LandR on Friday May 11, 2018 @03:05AM (#17045470)
In C# this would be wrong

     (-123 % 2 == 1).Dump();
Prints False.

% in C# is the remainder operator, not canonical modulus.

Re:Low Level Bit Hacks You Absolutely Must Know by tonyarkles on Friday May 11, 2018 @01:32PM (#17049249)
Javascript as well:

    >> -2 % 2
    -0
    >> -3 % 2
    -1
Edit: well, at least in Firefox JS. I have no idea if that's actually standard-mandated behaviour or if it's an implementation quirk. The -0 is especially weird (I had written odd but...)
Re:Low Level Bit Hacks You Absolutely Must Know by justin66 on Friday May 11, 2018 @01:40PM (#17049312)
This is pretty interesting. I don't use js but I took a quick look and apparently (x % 2) == 0 as a test for even integers should still work properly there, since a comparison of -0 and 0 will consider them equivalent.
Re:Low Level Bit Hacks You Absolutely Must Know by craftyguy on Friday May 11, 2018 @12:25PM (#17048615)
Well that's not confusing at all. Why would C# change the definition of a common operator character?
Re:Low Level Bit Hacks You Absolutely Must Know by bjoli on Friday May 11, 2018 @01:52PM (#17049417)
I think you would be surprised if you started looking around to see how how % is handled.
Re:Low Level Bit Hacks You Absolutely Must Know by justin66 on Friday May 11, 2018 @01:31PM (#17049239)
> I would think a "if (x % 2 == 1) //odd" would be within the realm of most CS applicants.

Yeah, if they can pass a fizzbuzz question they can probably get that. At which point it behooves the interviewer to explain why (x % 2) == 0 to find an even number is a better test...