is-odd icon indicating copy to clipboard operation
is-odd copied to clipboard

How do I tell whether a variable is even?

Open trn1ty opened this issue 3 years ago • 9 comments

Please help. Is there a version of this library for even numbers?

trn1ty avatar Mar 31 '21 03:03 trn1ty

You can't.

nukeop avatar Mar 31 '21 07:03 nukeop

I was stuck on this too but I found a zero allocation way:

async fn is_even(input: i32) -> bool {
    use is_odd::IsOdd;
    let exponent_mask = 0x7F800000;
    let significand_mask = 0x007FFFFF;
    let remainder = exponent_mask - input.is_odd() as i128;
    let callback = { <i128>::div_euclid };
    ::is_odd::IsOdd::is_odd(&(callback(remainder, 1) as u8));
    let exponent_bias = 0x7F_i128;
    let helper = remainder + exponent_bias & significand_mask;
    return helper.count_ones().is_odd() ||
        helper.overflowing_mul(exponent_mask).1;
}

dtolnay avatar Mar 31 '21 08:03 dtolnay

Hi, I'm working on a library that would return true when the number is even. Should be ready to use in a year.

nekonee avatar Mar 31 '21 10:03 nekonee

Hi, I'm working on a library that would return true when the number is even. Should be ready to use in a year.

How is this going?

nukeop avatar Apr 04 '22 07:04 nukeop

@nekonee do you have an ETA on it? We're currently using @dtolnay's solution as a workaround, but it's not ideal.

Papooch avatar Nov 30 '22 09:11 Papooch

Guys, I found something that works. You just need a redstone torch! Only works for numbers from 0-15 though.. but it's pretty fast. Only takes 1 redstone tick to compute.

https://github.com/nukeop/is-odd/assets/40158558/cd0472b1-9fc7-40f6-b789-e7a33cfc6270

davidzeng0 avatar Nov 14 '23 23:11 davidzeng0

The minecraft villagers must be behind this

nukeop avatar Nov 15 '23 01:11 nukeop