please dont rip this site

SX Microcontroller Bit Math Method


From: John Payson
comments from Andrew Warren of Fast Forward Engineering

;This routine will leave the parity of X in X.0
;while blenderizing most of the rest of X

	mov	W, <>X	;x =     abcdefgh  w =     efghabcd
	xor	X, W	;x =     abcdefgh  w =     efghabcd
			;    xor efghabcd
	mov	W, >>X	;x =     abcdefgh  w =     -abcdefg
			;    xor efghabcd      xor -efghabc
	xor	X, W	;x =     abcdefgh  w =     -abcdefg
			;    xor efghabcd      xor -efghabc
			;    xor -abcdefg
			;    xor -efghabc

	; at this point, the parity for half the bits
	; (a, b, e, and f) is in bit 2 of X, and the
	; parity for the other half (bits c, d, g, and h)
	; is in bit 0 of X.

	snb	X.2	; if the parity of (a,b,e,f) is 0,
                        ; then the parity of (a,b,c,d,e,f,g,h)
                        ; is equal to the parity of (c,d,g,h)...
                        ; which is already in bit 0, so skip ahead.

	inc	X	; otherwise, the parity of (a,b,e,f) is 1,
			; so the parity of (a,b,c,d,e,f,g,h) is
			; NOT equal to the parity of (c,d,g,h).
			; invert bit 0.

	; at this point, bit 0 contains the parity of
	; (a,b,c,d,e,f,g,h).

Kübek Tony [tony.kubek at FLINTAB.COM] says

; CALC_PARITY - Calculates parity of one byte, content is trashed,
result in lowest bit
; There '1' means ODD and '0' means EVEN
        SWAPF   ARG_BYTE, W 
        XORWF   ARG_BYTE, F 
        RRF     ARG_BYTE, W 
        XORWF   ARG_BYTE, F 
        BTFSC   ARG_BYTE, 2 
        INCF    ARG_BYTE, F 

First save the parity bit,either the 8'th bit in 7 bit comms. or the 9'th in 8 bit comms ( with parity ).

In the case of 7 bit comms do not forget to clear it prior to using the macro below.

Using for example 7 data bits even parity, the parity bit ( 8'th bit ) should always make sure that totally the byte will have an even number of bits.

So ( excluding the parity bit of cource ) maybe you have 3 [ones] bit in the received byte, then the parity bit is ( should be :-) ) set to '1', to achive an even number of bits.

Olin Lathrop [olin at], says:

Parity is a one-bit checksum for a string of bits. There are two kinds of parity, even and odd. Even parity is the one-bit sum of all the bits, and odd parity is the complement of that. A one-bit sum means the addition is carried out in a one-bit word, which is the same as the least significant bit of an addition using a larger word. This low bit therefore only indicates whether the sum was odd (1) or even (0). Therefore the even parity is 0 iff there are an even number of 1 bits in the string of bits being tested.

Note that one-bit addition is the same as XOR (make a truth table for addition and XOR if you're not conviced), so even parity can also be thought of as the XOR of all the bits being tested. That is how the compact example apparently from John Payson worked. First the high and low nibbles were XORed. Each bit in each nibble is now the parity of itself and the same bit in the other nibble. In other words, this reduces the problem to finding the parity of one of the nibbles. This value is XORed again with itself shifted right one bit. Bits 0 and 2 are now the parity of the upper and lower half of the nibbles. Bit 0 is then incremented if bit 2 is set, creating the combined parity in bit 0.

ODD parity is most useful for serial transmission like RS-232. This guarantees that each 9 bit word (8 data bits, 1 parity bit) sent contains at least one 0 bit and at least one 1 bit, which means all zeros or all ones is definitely an error condition. Parity isn't used as much today because it can only detect single bit errors. Most communications systems tend to use checksums for blocks of data with some sort of ACK/NACK higher level protocol.

file: /Techref/scenix/lib/math/bit/parity_sx.htm, 4KB, , updated: 2013/7/22 18:16, local time: 2024/5/25 08:20,

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF=""> SX Microcontroller Bit Math Method Parity</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

  PICList 2024 contributors:
o List host: MIT, Site host, Top posters @none found
- Page Editors: James Newton, David Cary, and YOU!
* Roman Black of Black Robotics donates from sales of Linistep stepper controller kits.
* Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters.
* Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated!
* Contributors: Richard Seriani, Sr.

Welcome to!