Is result odd or even?

Hooter

Senior Member
Folks - My brain is at half mast lately.
I am adding bits 0 to 7 together and I need to know if the result is odd or even - see below.

Code:
b6 = bit0 + bit1 + bit2 + bit3 + bit4 + bit5 + bit6 + bit7
I need to know if result (b6) is odd or even.
I have tried a few options but obviously not the correct one.
Any guidance is appreciated.
 

Pongo

Senior Member
You need modulus divide "//" which returns the remainder of the division

b6 = 8
b4 = b6//2
If b4 = 1 then
sertxd (#b6," is odd",cr,lf)
else
sertxd (#b6," is even",cr,lf)
endif
 

inglewoodpete

Senior Member
If you are using an X2 chip, you can use the NOB operator:
Code:
[color=Purple]b6 [/color][color=DarkCyan]= NOB [/color][color=Purple]b0 [/color][color=DarkCyan]and [/color][color=Navy]1[/color]
M2s are a bit more tedious:
Code:
[color=Purple]b6 [/color][color=DarkCyan]= [/color][color=Purple]bit0 [/color][color=DarkCyan]+ [/color][color=Purple]bit1 [/color][color=DarkCyan]+ [/color][color=Purple]bit2 [/color][color=DarkCyan]+ [/color][color=Purple]bit3 [/color][color=DarkCyan]+ [/color][color=Purple]bit4 [/color][color=DarkCyan]+ [/color][color=Purple]bit5 [/color][color=DarkCyan]+ [/color][color=Purple]bit6 [/color][color=DarkCyan]+ [/color][color=Purple]bit7 [/color][color=DarkCyan]And [/color][color=Navy]1[/color]
 

westaust55

Moderator
Or another 1 liner (at 19 bytes):
bit10 = bit7 XOR bit6 XOR bit5 XOR bit4 XOR bit3 XOR bit2 XORbit 1 XOR bit0 ; bit10 = 1 if odd and 0 if even
 

Buzby

Senior Member
If you use another of the first four bytes instead of b6, you can do something like this :

Code:
  b1 = bit0 + bit1 + bit2 + bit3 + bit4 + bit5 + bit6 + bit7

  if bit8 = 0 then
    ' b1 is even
  else
    ' b1 is odd
  endif
( See page 13 in manual 2 )

Cheers,

Buzby
 

Buzby

Senior Member
And even one bit...
bit8 = bit0 + bit1 + bit2 + bit3 + bit4 + bit5 + bit6 + bit7
Yes, but you only know if the result is odd or even, you don't have the byte value.

I suppose it really depends on whether the OP needs the 'value' of the sum and it's 'odd/even' sign, or just the sign.
 

erco

Senior Member
Code:
b7=b6/2*2   ' integer math drops the half when dividing odd numbers
if b7=b6 then [I]even[/I]
else [I]odd[/I]
The programmers and logicians here have come up with the shortest and cleverest ways to test for "oddness". I can't add anything there, but at the other end of the cleverness spectrum is the old "divide by two then multiply by two" test. The long way round, but perhaps easier for simpletons like me to grasp at a quick glance at 2 AM.

Code:
for b6=0 to 255
sertxd (#b6,"  ")
b7=b6/2*2  ' integer math drops the half when dividing odd numbers
if b7=b6 then sertxd("even",13,10)
else sertxd("odd",13,10)
endif
pause 200
next
 

inglewoodpete

Senior Member
Code:
b7=b6/2*2   ' integer math drops the half when dividing odd numbers
if b7=b6 then [I]even[/I]
else [I]odd[/I]
The programmers and logicians here have come up with the shortest and cleverest ways to test for "oddness". I can't add anything there, but at the other end of the cleverness spectrum is the old "divide by two then multiply by two" test. The long way round, but perhaps easier for simpletons like me to grasp at a quick glance at 2 AM.

Code:
for b6=0 to 255
sertxd (#b6,"  ")
b7=b6/2*2  ' integer math drops the half when dividing odd numbers
if b7=b6 then sertxd("even",13,10)
else sertxd("odd",13,10)
endif
pause 200
next
Unless I'm mistaken, I think you are trying to crack a nut with a sledgehammer. Hooter was trying to determine if he had an odd or even number of bits set in a byte variable. Hence my reply in post #3: just AND the count of set bits with 1
 

lbenson

Senior Member
And further to IP's elegant solution, if you don't want to use another variable and don't mind destroying your original data:

b0 = bit0 + bit1 + bit2 + bit3 + bit4 + bit5 + bit6 + bit7
if bit0 = 1 then
...

or (destructive only of bit0)

bit0 = bit0 + bit1 + bit2 + bit3 + bit4 + bit5 + bit6 + bit7
if bit0 = 1 then
...
Code:
' 08evenoddbits
#picaxe 08m2
#terminal 4800

for b1 = 0 to 255
  b0 = b1
  bit0 = bit0 + bit1 + bit2 + bit3 + bit4 + bit5 + bit6 + bit7
  if bit0 = 1 then
    sertxd(#b1,": %",#bit15,#bit14,#bit13,#bit12,#bit11,#bit10,#bit9,#bit8," has odd number of bits on",cr,lf)
  else
    sertxd(#b1,": %",#bit15,#bit14,#bit13,#bit12,#bit11,#bit10,#bit9,#bit8," has even number of bits on",cr,lf)
  endif
next b1
 
Last edited:

hippy

Technical Support
Staff member
Fastest solution is probably -

Code:
Read b0, b1
If b1 = 1 Then ...
Though it does mean using all Data Eeprom and having to populate that. Table could also be used when available.

A variation on erco's 'divide by 2' theme ...

Code:
b1 = bit7 + bit6 + bit5 + bit4 + bit3 + bit2 + bit1 + bit0 
b2 = b1 * $80 / $80
If b2 = 1 Then ...
Or, faster ...

Code:
b1 = bit7 + bit6 + bit5 + bit4 + bit3 + bit2 + bit1 + bit0 
b2 = b1 * $80
If b2 >= $80 Then ...
 
Last edited:

Hooter

Senior Member
Thanks for all of the responses. I ran with Inglewoodpetes suggestion as it was close to what I already had and it works spot on.
 
Top