In the section Humanities to the question Is Zero even or odd? And why given by the author KATERINA the best answer is Parity in number theory is a characteristic of an integer that determines its ability to be divisible by two. If an integer is divisible by two, it is called even (examples: 2, 28, -8, 40), if not, it is called odd (examples: 1,3, 75, -19). Zero is considered even number.
An even number is an integer that is divisible by 2 without a remainder: …−4,-2,0,2,4,6,8…
An odd number is an integer that is not divisible by 2 without a remainder: …−3,−1,1,3,5,7,9…
In other words, even and odd numbers are elements of the residue and modulo 2 classes, respectively.

Reply from Valentina Dubkovskaya[guru]
Even. Because it is divisible by 2.


Reply from Yofya Erina[guru]
Yes. But mother, by the way, exact science, not humanitarian!


Reply from User deleted[guru]
All even numbers are divisible by 2, including 0.


Reply from James Lukash[guru]
Apparently, zero is still an even number, if the wiki says so in conjunction with TSB, although I believed that zero stands apart from the rest of the number series and is neither even nor odd


Reply from L[active]
zero is absolute and self-sufficient. why divide it?


Reply from Yergey Sergeev[active]
Finally, in my opinion, zero is not a number and that the section is selected humanities- that's right. Zero is a concept, a definition, and the fact that it is divided by 2 does not mean anything. Zero is the same as infinity, only in reverse. And you can think about this topic endlessly. And if anyone is interested, they can look for my “Reflections on Eternity”, but on the Internet they call me Gringo


Reply from Danil "stager" Voronov[active]
Sonya Erina User menu Expert (307)1 minute ago (link)ComplainComplainYes. But mating, by the way, is an exact science, not a humanities one! o_0

  • Odd number- an integer that doesn't share without remainder: …, −3, −1, 1, 3, 5, 7, 9, …

If m is even, then it can be represented in the form m = 2 k, and if odd, then in the form m = 2 k + 1, Where k \in \mathbb Z.

History and culture

The concept of parity of numbers has been known since ancient times and has often been given a mystical meaning. In Chinese cosmology and natural philosophy, even numbers correspond to the concept of “yin”, and odd numbers correspond to “yang”.

IN different countries There are traditions associated with the number of flowers given. For example, in the USA, Europe and some eastern countries it is believed that an even number of flowers given brings happiness. In Russia and the CIS countries, it is customary to bring an even number of flowers only to the funeral of the dead. However, in cases where there are many flowers in the bouquet (usually more), the evenness or oddness of their number no longer plays any role. For example, it is quite acceptable to give a lady a bouquet of 12, 14, 16, etc. flowers or sections of a bush flower that have many buds, in which they, in principle, cannot be counted. This is especially true for the larger number of flowers (cuts) given on other occasions.

Practice

In higher educational institutions with complex graphs educational process Even and odd weeks apply. Within these weeks, the schedule of training sessions and, in some cases, their start and end times differ. This practice is used to distribute the load evenly across classrooms, educational buildings and for the rhythm of classes in disciplines with a low classroom load (once every 2 weeks)

Train schedules use even and odd train numbers, depending on the direction of travel (direct or reverse). Accordingly, even/odd indicates the direction in which the train passes through each station.

Even and odd days of the month are sometimes associated with train schedules that are organized every other day.

Write a review about the article "Even and Odd Numbers"

Notes

Links

  • Sequence A005408 in OEIS: odd numbers
  • Sequence A005843 in OEIS: even numbers
  • Sequence A179082 in OEIS: even numbers with an even sum of digits in decimal notation

Excerpt describing Even and Odd Numbers

“Well, well,” said Prince Andrei, turning to Alpatych, “tell me everything, as I told you.” - And, without answering a word to Berg, who fell silent next to him, he touched his horse and rode into the alley.

The troops continued to retreat from Smolensk. The enemy followed them. On August 10, the regiment, commanded by Prince Andrei, passed along the high road, past the avenue leading to Bald Mountains. The heat and drought lasted for more than three weeks. Every day, curly clouds walked across the sky, occasionally blocking the sun; but in the evening it cleared again, and the sun set in a brownish-red haze. Only heavy dew at night refreshed the earth. The bread that remained on the root burned and spilled out. The swamps are dry. The cattle roared from hunger, not finding food in the sun-burnt meadows. Only at night and in the forests there was still dew and there was coolness. But along the road, along the high road along which the troops marched, even at night, even through the forests, there was no such coolness. The dew was not noticeable on the sandy dust of the road, which had been pushed up more than a quarter of an arshin. As soon as dawn broke, the movement began. The convoys and artillery walked silently along the hub, and the infantry were ankle-deep in soft, stuffy, hot dust that had not cooled down overnight. One part of this sand dust was kneaded by feet and wheels, the other rose and stood as a cloud above the army, sticking into the eyes, hair, ears, nostrils and, most importantly, into the lungs of people and animals moving along this road. The higher the sun rose, the higher the cloud of dust rose, and through this thin, hot dust in the sun, covered by clouds, could be seen with a simple eye. The sun appeared as a large crimson ball. There was no wind, and people were suffocating in this still atmosphere. People walked with scarves tied around their noses and mouths. Arriving at the village, everyone rushed to the wells. They fought for water and drank it until they were dirty.
Prince Andrei commanded the regiment, and the structure of the regiment, the welfare of its people, the need to receive and give orders occupied him. The fire of Smolensk and its abandonment were an era for Prince Andrei. A new feeling of bitterness against the enemy made him forget his grief. He was entirely devoted to the affairs of his regiment, he was caring for his people and officers and affectionate with them. In the regiment they called him our prince, they were proud of him and loved him. But he was kind and meek only with his regimental soldiers, with Timokhin, etc., with completely new people and in a foreign environment, with people who could not know and understand his past; but as soon as he encountered one of his former ones, from the staff, he immediately bristled again; he became angry, mocking and contemptuous. Everything that connected his memory with the past repulsed him, and therefore he tried in the relations of this former world only not to be unfair and to fulfill his duty.
True, everything seemed to Prince Andrei in a dark, gloomy light - especially after they left Smolensk (which, according to his concepts, could and should have been defended) on August 6, and after his father, who was sick, had to flee to Moscow and throw the Bald Mountains, so beloved, built and inhabited by him, for plunder; but, despite this, thanks to the regiment, Prince Andrei could think about something else, completely independent of general issues subject - about your regiment. On August 10, the column in which his regiment was located reached Bald Mountains. Prince Andrey received news two days ago that his father, son and sister had left for Moscow. Although Prince Andrei had nothing to do in Bald Mountains, he, with his characteristic desire to relieve his grief, decided that he should stop by Bald Mountains.
He ordered a horse to be saddled and from the transition rode on horseback to his father’s village, in which he was born and spent his childhood. Driving past a pond, where dozens of women were always talking, beating rollers and rinsing their laundry, Prince Andrei noticed that there was no one on the pond, and a torn raft, half filled with water, was floating sideways in the middle of the pond. Prince Andrei drove up to the gatehouse. There was no one at the stone entrance gate, and the door was unlocked. The garden paths were already overgrown, and calves and horses were walking around the English park. Prince Andrei drove up to the greenhouse; the glass was broken, and some trees in tubs were knocked down, some withered. He called out to Taras the gardener. Nobody responded. Walking around the greenhouse to the exhibition, he saw that the wooden carved fence was all broken and the plum fruits were torn from their branches. An old man (Prince Andrei saw him at the gate as a child) sat and weaved bast shoes on a green bench.
He was deaf and did not hear Prince Andrei's entrance. He was sitting on a bench where he liked to sit old prince, and near him a sash was hung on the branches of a broken and dried magnolia.
Prince Andrei drove up to the house. Several linden trees in the old garden had been cut down, one piebald horse with a foal walked in front of the house between the rose trees. The house was boarded up with shutters. One window downstairs was open. The yard boy, seeing Prince Andrei, ran into the house.
Alpatych, having sent his family away, remained alone in Bald Mountains; he sat at home and read the Lives. Having learned about the arrival of Prince Andrey, he, with glasses on his nose, buttoned up, left the house, hastily approached the prince and, without saying anything, began to cry, kissing Prince Andrey on the knee.



even odd c++> (6)

Adding two integers adds their parity, so the solution is simple:

If ((j + m) % 2)

Unsigned wraparound does not violate this property, since it is done modulo UINT_MAX+1 which is an even number.

This solution does not depend on any implementation-specific details such as negative numeric representation.

Footnote: I'm struggling to understand why so many other answers complicate the problem with bit shifts, bit complements, XOR, etc etc. Unfortunately IMO it is sometimes glorified in the C or C++ communities for writing tricky code instead of simple code.

I have an int m and an unsigned int j and want to determine if they are even or odd.

I used to use

If((int(j)+m)%2)

to catch the case that only one is odd. But I'm concerned that casting on int incorrectly changes the odd parity of j .

I know this

If(j%2!=m%2)

doesn't work because "m%2" will generate -1 when m is negative, which will always evaluate to true regardless of the value of j%2 .

If (1 & (i ^ j)) ( // Getting here if i is even and j is odd // or if i is odd and j is even )

^ is an exclusive-or bitwise operator that tests every bit in both numbers if they have the same value. For example, if the binary representation of i is 0101 and j is 1100, then i^j will evaluate to 1001 since their first and last bits are different, whereas the middle bits are the same.

& is a bitwise and operator that tests each bit in both numbers if they are both 1 .

Since only the last bit of each number determines whether it is even or odd, i^j will evaluate...xxx0 if they are both even or odd, and...xxx1 otherwise (x s doesn't matter, we aren't interested anyway, they look at them). Since 1 is really...0001 , 1 & (i^j) evaluates to 0 if i and j are even or odd, and 1 otherwise.

This works on any combination of unsigned numbers, 2s's complement and sign and magnitude, but not on the rare 1s's complement if exactly one is negative.

This can be simplified:

If(!(j%2)!=!(m%2)) if(bool(j%2)!=bool(j%2))

If ((abs(m) % 2) != (j % 2))

be sure to include math.h

#include

An absolute value will take the sign bit, which is the leftmost bit in memory.

Converting signed to unsigned is fine and defined in C99.

Bitwise operators must also work with the C99 compiler, and signed with a smaller maximum value is converted to a larger one (signed unsigned).

INT_MAX unsigned int that is greater than INT_MAX in int is not guaranteed to return a reasonable value. The result is undetermined.

Casting an int to an unsigned int always results in a certain behavior - it does the math mod 2^k for some k large enough that every positive int is less than 2^k .

If((int(j)+m)%2)

there must be

If((j+unsigned(m))%2)

If((j%2)==(unsigned(m)%2))

this is the easiest way to see if both have the same parity. Changing to unsigned aka mod 2^k will preserve parity, and unsigned %2 will correctly return parity (rather than negative parity).

Don't be too smart

Do any of them have problems?

if(!(j%2)!=!(m%2)) if(bool(j%2)!=bool(j%2))

One of the problems I see is readability. It may not be obvious to someone else (or your future self) what they should be doing or what they are actually doing.

You could be more expressive by passing some extra lines:

#include const bool fooIsEven = foo % 2 == 0; const bool barIsEven = std::abs(bar) % 2 == 0; if (fooIsEven == barIsEven) ( // ... )

We will also consider the possibility of implementing a properly named function that provides a comparison of the parity of two given integral types. This not only cleans up your code, but also prevents you from repeating yourself.

Change: Replaced by push call to std::abs