-1

c program to check odd or even without using modulus operator

We can check a number is even or odd without using modulus and division operator in c program The another method is using shift operators number >> 1) <<1==number then it is even number,can someone explaain this?

Ashika
  • 3
  • 2
  • 1
    Don't do that with a negative number - it is implementation defined: https://stackoverflow.com/questions/1857928/right-shifting-negative-numbers-in-c – Jerry Jeremiah Aug 25 '22 at 01:26
  • If it is a positive number then right shifting by one binary bit is the same as dividing by two (but it is an integer so there is no fractional part) and left shifting is the same as multiplying by two. If you do an integer divide by two and then multiply by two the result will only equal the original number if it was even. – Jerry Jeremiah Aug 25 '22 at 01:28
  • 1
    @JerryJeremiah: I'm not 100% on this, but I believe it doesn't matter how the implementation defines it, since they immediately shift it back to the left (whether it zero-fills or one-fills is irrelevant when the filled in bit is immediately discarded anyway). – ShadowRanger Aug 25 '22 at 01:31
  • 1
    @ShadowRanger: Right-shift of a negative value is fully implementation-defined. The implementation could define it to be zero or `INT_MAX` or the integer nearest the square root of the absolute value of the operand. But even if the implementation merely defines it as an arithmetic shift, the subsequent left-shift of the negative value is completely undefined by the C standard. – Eric Postpischil Aug 25 '22 at 01:44
  • Another way to check for and even number is `(number & 1) == 0`, or `!(number & 1)`. – Tom Karzes Aug 25 '22 at 01:53

4 Answers4

2

A right shift by x is essentially a truncating division by 2x.

A left shift by x is essentially a multiplication by 2x.

6 ⇒ 3 ⇒ 6
7 ⇒ 3 ⇒ 6

If this produces the original number, it's even.

ikegami
  • 367,544
  • 15
  • 269
  • 518
1

An unsigned number is odd if its lowest (1) bit is 1. What this does is shift right by 1, and then right by one, effictively zeroing out this first bit. If that bit was already a 0 (i.e. the number is even), then the number doesn't change and the == passes. If it was a 1 then its now a zero and the equality check fails.

A better, more obvious implementation of this logic would be:

if((number & 0x1) == 0)

which checks directly whether the bit is a 0 (i.e. the number is even)

Sinkingpoint
  • 7,449
  • 2
  • 29
  • 45
1

We can check a number is even or odd without using modulus and division operator in c program The another method is using shift operators number >> 1) <<1==number then it is even number

Do not use if(( number >> 1) <<1==number) as it risks implementation defined behavior when number < 0.

Only for the pedantic

This is likely incorrect on rare machines that use ones' complement int encoding.

Of course such beast are so rare these days and the next version of C, C2x, is expected to no longer support non-2's complement encoding.

Alternative

Instead code can use:

is_odd = number & 1LLU;

This will convert various possible integer types of number into unsigned long long and then perform a simple mask of the least significant bit (the one's place). Even with negatives values in any encoding will convert mathematically to an unsigned value with the same even-ness.

Modulus operator??

... using modulus and division operator ...

In C there is no operator defined as modulus. There is %, which results in the remainder.
See What's the difference between “mod” and “remainder”?.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

Right-shifting by one shifts off the low bit, left-shifting back restores the value without that low bit.

All odd numbers end in a low bit of 1, which this removes, so the equality comparison only returns true for even numbers, where the removal and re-adding of the low 0 bit does not change the value.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271