Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

c - How do I check if an integer is even or odd using bitwise operators

How do I check if an integer is even or odd using bitwise operators

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Consider what being "even" and "odd" means in "bit" terms. Since binary integer data is stored with bits indicating multiples of 2, the lowest-order bit will correspond to 20, which is of course 1, while all of the other bits will correspond to multiples of 2 (21 = 2, 22 = 4, etc.). Gratuituous ASCII art:

NNNNNNNN
||||||||
|||||||+?? bit 0, value =   1 (2
0) ||||||+??? bit 1, value = 2 (21) |||||+???? bit 2, value = 4 (22) ||||+????? bit 3, value = 8 (23) |||+?????? bit 4, value = 16 (24) ||+??????? bit 5, value = 32 (25) |+???????? bit 6, value = 64 (26) +????????? bit 7 (highest order bit), value = 128 (27) for unsigned numbers, value = -128 (-27) for signed numbers (2's complement)

I've only shown 8 bits there, but you get the idea.

So you can tell whether an integer is even or odd by looking only at the lowest-order bit: If it's set, the number is odd. If not, it's even. You don't care about the other bits because they all denote multiples of 2, and so they can't make the value odd.

The way you look at that bit is by using the AND operator of your language. In C and many other languages syntactically derived from B (yes, B), that operator is &. In BASICs, it's usually And. You take your integer, AND it with 1 (which is a number with only the lowest-order bit set), and if the result is not equal to 0, the bit was set.

I'm intentionally not actually giving the code here, not only because I don't know what language you're using, but because you marked the question "homework." :-)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...