Problem with the 'and' command.

Hooter

Senior Member
Folks - I am having issues with the following code.
I should be getting a 0 when using the 'and' command with the number 4060 (111111011100 in binary) which a calculator gives me
but when run in the simulator and in a 08M2 I get a result of 1.

The variable W1 holds a 26 bit number which changes during the running of the program.
I need to test 12 of the 26 bits for parity - a 1 or a 0 - which determines how the remainder of the program will flow.
From a previous post of mine I - rightly or wrongly - understood you could select which bits you wanted and 'and' them with one to
get the result I needed.

Code:
main:
w1 = 4060 '  Binary 111111011100 - bit27 = 1, bit26 = 1,.....bit21 = 0, bit20 = 1 etc.

b1 =  bit27 + bit26 + bit25 + bit24 + bit23 + bit22 + bit21 + bit20 + bit19 + bit18 + bit17+ bit16 And 1 
sertxd (#bit27,#bit26,#bit25,#bit24,#bit23,#bit22,#bit21,#bit20,#bit19,#bit18,#bit17,#bit16,13,10)
sertxd ("Parity is ",#b1,13,10)
pause 5000
goto main
What am I missing here.
As always any help is appreciated.
 
Last edited:

premelec

Senior Member
Not quite sure what you are up to but note the BIT# == 0 or 1 so BIT + BIT = 0 to 2... So for 16 bits you can get anywhere from 0 to 16 adding the bit values...
 

Rick100

Senior Member
Hello Hooter,

It looks like the bits add up to 9, so 9 AND 1 = 1. The result looks right to me.

Good luck,
Rick
 

Hooter

Senior Member
Premelec - The variable W1 holds a 26 bit number which changes during the running of the program.
I need to test 12 of the 26 bits for parity - a 1 or a 0 - which determines how the remainder of the program will flow.
From a previous post of mine I - rightly or wrongly - understood you could select which bits you wanted and 'and' them with one to get the result I needed.
 

inglewoodpete

Senior Member
Using the formula you posted in #1, a number with an even number of '1' bits will give a zero. An odd number of bits will return a one.

4060 (%0000 1111 1101 1100) has an odd number of set bits: a total of 9 (%1001).
The result of the addition, 9, AND 1 gives a result of 1 (%1001 AND %0001 = %0001)
 

hippy

Technical Support
Staff member
I need to test 12 of the 26 bits for parity - a 1 or a 0
What do you mean though by "test for parity" ?

You can add the bits up and AND with 1 and you can determine if the number of bits is odd (1) or even (0) which is what you are doing and that works as expected.

"Parity" without qualification is ambiguous; you can have "odd parity" or "even parity". One case expects the number of bits to be odd, the other even.

If you want a 1 when you are getting 0, you can simply invert the bit.
 

westaust55

Moderator
If the lowest bit of a number is 1 it's an odd number, if its 0 it's even.
That applies for the value but not parity. Parity is based on the number of bits with a value of '1'.
At post 1 the topic is parity but as hippy mentions above it is unclear whether odd or even parity is sought.
 

Hooter

Senior Member
Sorry guys - I haven't explained my request well at all. I shall try again. I have included a couple of screenshots which may help.
I have made a device which emulates a 26 bit Wiegand proximity card for testing card reader inputs on access control systems at my place of work.
The original version of this device had a fixed site code and user number - ie. site code 123 and user number 456.
The 26 bit Wiegand format consists of 26 bits. Bits 1 and 26 are parity bits. Bit 1 is even parity bit for the first 13 bits
and bit 26 is odd parity for the second 13 bits.
Bits 2 to 9 are the site code and bits 10 to 25 are the user number.
As the user number and site code are fixed the parity bits were easy to apply.
This worked perfectly.
I then decided it would be beneficial to be able to program any site code and any user number.
As the site code and user numbers were now changing so would the parity bits.
By adding the bits 2 to 13 and 'anding' the result with 1 this would then determine whether I needed to add a 0 or 1 to bit 1 as the site code parity bit.
This calculation was also necessary for adding bits 14 to 25, 'anding' them with 1 to determine the user number parity bit 26 and invert it if necessary.
This is where the confusion came in.
I was getting different results from the calculation when using a calculator than I was in the Picaxe Simulator - hence the posting.
I think I have garnered enough insight from all of your postings to sort this out.
Sorry for any confusion.
Wiegand.JPGMenu.JPG
 

BESQUEUT

Senior Member
Maybe something like this :
Code:
[color=Navy]#simspeed 10
#macro [/color][color=Black]Wiegand[/color][color=Blue]([/color][color=Black]Facility,Card,Expected[/color][color=Blue])[/color]

[color=Purple]w0[/color][color=DarkCyan]=[/color][color=Black]Facility[/color]
[color=Purple]w1[/color][color=DarkCyan]=[/color][color=Black]Card[/color]
[color=Blue]sertxd ([/color][color=Navy]13[/color][color=Black],[/color][color=Navy]10[/color][color=Black],[/color][color=Navy]13[/color][color=Black],[/color][color=Navy]10[/color][color=Black],[/color][color=Red]"DATA in :  "[/color][color=Blue])
sertxd([/color][color=Black]#[/color][color=Purple]bit7[/color][color=Black],#[/color][color=Purple]bit6[/color][color=Black],#[/color][color=Purple]bit5[/color][color=Black],#[/color][color=Purple]bit4[/color][color=Black],#[/color][color=Purple]bit3[/color][color=Black],#[/color][color=Purple]bit2[/color][color=Black],#[/color][color=Purple]bit1[/color][color=Black],#[/color][color=Purple]bit0[/color][color=Blue])
sertxd([/color][color=Black]#[/color][color=Purple]bit31[/color][color=Black],#[/color][color=Purple]bit30[/color][color=Black],#[/color][color=Purple]bit29[/color][color=Black],#[/color][color=Purple]bit28[/color][color=Black],#[/color][color=Purple]bit27[/color][color=Black],#[/color][color=Purple]bit26[/color][color=Black],#[/color][color=Purple]bit25[/color][color=Black],#[/color][color=Purple]bit14[/color][color=Black],#[/color][color=Purple]bit23[/color][color=Black],#[/color][color=Purple]bit22[/color][color=Black],#[/color][color=Purple]bit21[/color][color=Black],#[/color][color=Purple]bit20[/color][color=Black],#[/color][color=Purple]bit19[/color][color=Black],#[/color][color=Purple]bit18[/color][color=Black],#[/color][color=Purple]bit17[/color][color=Black],#[/color][color=Purple]bit16[/color][color=Blue])
sertxd ([/color][color=Red]" "[/color][color=Black], #[/color][color=Purple]w0[/color][color=Black], [/color][color=Red]" "[/color][color=Black], #[/color][color=Purple]w1[/color][color=Black],[/color][color=Navy]13[/color][color=Black],[/color][color=Navy]10[/color][color=Blue])[/color]

[color=Purple]bit15[/color][color=DarkCyan]=[/color][color=Purple]bit7[/color][color=DarkCyan]+[/color][color=Purple]bit6[/color][color=DarkCyan]+[/color][color=Purple]bit5[/color][color=DarkCyan]+[/color][color=Purple]bit4[/color][color=DarkCyan]+[/color][color=Purple]bit3[/color][color=DarkCyan]+[/color][color=Purple]bit2[/color][color=DarkCyan]+[/color][color=Purple]bit1[/color][color=DarkCyan]+[/color][color=Purple]bit0
bit14[/color][color=DarkCyan]=[/color][color=Purple]bit31[/color][color=DarkCyan]+[/color][color=Purple]bit30[/color][color=DarkCyan]+[/color][color=Purple]bit29[/color][color=DarkCyan]+[/color][color=Purple]bit28[/color][color=DarkCyan]+[/color][color=Purple]bit27[/color][color=DarkCyan]+[/color][color=Purple]bit26[/color][color=DarkCyan]+[/color][color=Purple]bit25[/color][color=DarkCyan]+[/color][color=Purple]bit24[/color][color=DarkCyan]+[/color][color=Purple]bit23[/color][color=DarkCyan]+[/color][color=Purple]bit22[/color][color=DarkCyan]+[/color][color=Purple]bit21[/color][color=DarkCyan]+[/color][color=Purple]bit20[/color][color=DarkCyan]+[/color][color=Purple]bit19[/color][color=DarkCyan]+[/color][color=Purple]bit18[/color][color=DarkCyan]+[/color][color=Purple]bit17[/color][color=DarkCyan]+[/color][color=Purple]bit16[/color][color=DarkCyan]+[/color][color=Navy]1[/color]

[color=Blue]sertxd ([/color][color=Red]"Result  : "[/color][color=Blue])
sertxd([/color][color=Black]#[/color][color=Purple]bit15[/color][color=Black],#[/color][color=Purple]bit7[/color][color=Black],#[/color][color=Purple]bit6[/color][color=Black],#[/color][color=Purple]bit5[/color][color=Black],#[/color][color=Purple]bit4[/color][color=Black],#[/color][color=Purple]bit3[/color][color=Black],#[/color][color=Purple]bit2[/color][color=Black],#[/color][color=Purple]bit1[/color][color=Black],#[/color][color=Purple]bit0[/color][color=Blue])
sertxd([/color][color=Black]#[/color][color=Purple]bit31[/color][color=Black],#[/color][color=Purple]bit30[/color][color=Black],#[/color][color=Purple]bit29[/color][color=Black],#[/color][color=Purple]bit28[/color][color=Black],#[/color][color=Purple]bit27[/color][color=Black],#[/color][color=Purple]bit26[/color][color=Black],#[/color][color=Purple]bit25[/color][color=Black],#[/color][color=Purple]bit24[/color][color=Black],#[/color][color=Purple]bit23[/color][color=Black],#[/color][color=Purple]bit22[/color][color=Black],#[/color][color=Purple]bit21[/color][color=Black],#[/color][color=Purple]bit20[/color][color=Black],#[/color][color=Purple]bit19[/color][color=Black],#[/color][color=Purple]bit18[/color][color=Black],#[/color][color=Purple]bit17[/color][color=Black],#[/color][color=Purple]bit16[/color][color=Black],#[/color][color=Purple]bit14[/color][color=Black],[/color][color=Navy]13[/color][color=Black],[/color][color=Navy]10[/color][color=Blue])
sertxd ([/color][color=Red]"Expected: "[/color][color=Black],Expected,[/color][color=Navy]13[/color][color=Black],[/color][color=Navy]10[/color][color=Blue])[/color]
[color=Navy]#endmacro[/color]

[color=Black]Wiegand[/color][color=Blue]([/color][color=Navy]123[/color][color=Black],[/color][color=Navy]65533[/color][color=Black],[/color][color=Red]"00111101111111111111111010 16252922"[/color][color=Blue])[/color]
[color=Black]Wiegand[/color][color=Blue]([/color][color=Navy]124[/color][color=Black],[/color][color=Navy]65533[/color][color=Black],[/color][color=Red]"10111110011111111111111010 49938426"[/color][color=Blue])[/color]
[color=Black]Wiegand[/color][color=Blue]([/color][color=Navy]123[/color][color=Black],[/color][color=Navy]65532[/color][color=Black],[/color][color=Red]"00111101111111111111111001 16252921"[/color][color=Blue])[/color]
[color=Black]Wiegand[/color][color=Blue]([/color][color=Navy]0[/color][color=Black],[/color][color=Navy]0[/color][color=Black],[/color][color=Red]"00000000000000000000000001 1"[/color][color=Blue])[/color]
Code:
DATA in :  011110111111111011111101 123 65533
Result  : 00111101111111111111111010
Expected: 00111101111111111111111010 16252922


DATA in :  011111001111111011111101 124 65533
Result  : 10111110011111111111111010
Expected: 10111110011111111111111010 49938426


DATA in :  011110111111111011111100 123 65532
Result  : 00111101111111111111111001
Expected: 00111101111111111111111001 16252921


DATA in :  000000000000000000000000 0 0
Result  : 00000000000000000000000001
Expected: 00000000000000000000000001 1
Expecteds results calculated with wiegand_calc then converted to binary with Excel.
 
Last edited:

binary1248

Senior Member
Hooter, that last post of yours made it clear with the screen shots, up till then I kept thinking (I'm an idiot, I don't understand this 28 bit register, B0 is only 16 bits). Now I see the problem, but I am just following to learn, I have no solution. Maybe I'll learn something new.
Paul
 

hippy

Technical Support
Staff member
This is how I would do it. The raw Wiegand value is put into w1 and w0, bit25 to bit0. The oBit and eBit are the odd and even parity bits, set to 1 when correct, 0 when not. The fPart and cPart the extracted facility and card values.

Code:
Symbol eBit  = w2
Symbol oBit  = w3

Symbol fPart = w4
Symbol cPart = w5

Do

  ;     2  2222 2111  1111 111
  ;     5  4321 0987  6543 2109 8765 4321  0  
  ;
  ;     .---------.               .--------.
  ;    \|/ _______|_______ _______|______ \|/
  ;     E  FFFF FFFF  CCCC CCCC CCCC CCCC  O
  ;
  ;     0  0111 1011  1111 1111 1111 1101  0 : F=123 C=65533


  ;     2222221111         111111
  ;     5432109876         5432109876543210
  w1 = %0011110111 : w0 = %1111111111111010

  eBit = bit25 ^ _
         bit24 ^ bit23 ^ bit22 ^ bit21 ^ _
         bit20 ^ bit19 ^ bit18 ^ bit17 ^ _
         bit16 ^ bit15 ^ bit14 ^ bit13 ^ 1

  oBit = bit12 ^ bit11 ^ bit10 ^ bit9  ^ _
         bit8  ^ bit7  ^ bit6  ^ bit5  ^ _
         bit4  ^ bit3  ^ bit2  ^ bit1  ^ _
         bit0

  fPart = w1 / 2 & $FF

  cPart = w0 / 2
  cPart = bit16 * $8000 | cPart

  SerTxd( "E=", #eBit,  " " ) : If eBit = 1 Then : SerTxd( "(OK) " ) : End If
  SerTxd( "O=", #oBit,  " " ) : If oBit = 1 Then : SerTxd( "(OK) " ) : End If
  SerTxd( "F=", #fPart, " " )
  SerTxd( "C=", #cPart, CR, LF )

  Pause 1000

Loop
eBit and oBit are defined as words here but can be byte variables.
 
Top