Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save goliatone/ff8f68e33e980c4f25a3c6a8d5381dfc to your computer and use it in GitHub Desktop.
Save goliatone/ff8f68e33e980c4f25a3c6a8d5381dfc to your computer and use it in GitHub Desktop.
Calculate parity bit for a string representation of a binary number, i.e 0101011101
var FACILITY_CODE = /(\d+)$/;
var WIEGAND_LENGTH = /^Wiegand (\d+)/i;
function getFacilityCode(format=''){
let match = format.match(FACILITY_CODE);
if(!match) return undefined;
let code = match[1];
return parseInt(code);
}
function getWiegandLength(format=''){
let match = format.match(WIEGAND_LENGTH);
if(!match) return undefined;
let code = match[1];
return parseInt(code);
}
console.assert(getFacilityCode('Wiegand 26-bit facility code 203') === 203);
console.assert(getWiegandLength('Wiegand 26-bit facility code 203') == 26);
console.assert(getFacilityCode('Wiegand 26 bit facility code 100') === 100);
console.assert(getWiegandLength('Wiegand 26 bit facility code 100') === 26);
console.assert(getFacilityCode('Wiegand 37 bit Facility Code 120') === 120);
console.assert(getWiegandLength('Wiegand 37 bit Facility Code 120') === 37);
console.assert(getFacilityCode('Wiegand 39 bit Bluetooth Facility Code 3') === 3);
console.assert(getWiegandLength('Wiegand 39 bit Bluetooth Facility Code 3') === 39);
/**
* Calculate parity bit for a binary sequence.
* Given a string representing a binary number,
* this will calculate the even or odd parity bit.
*
* In the case of **even** parity, for a given set of
* bits, the occurrences of bits whose value is 1
* is counted. If that count is odd, the parity bit
* value is set to 1, making the total count of
* occurrences of 1's in the whole set
* (including the parity bit) an even number.
* If the count of 1's in a given set of bits is
* already even, the parity bit's value is 0.
*
* In the case of odd parity, the coding is
* reversed. For a given set of bits, if the count
* of bits with a value of 1 is even, the parity
* bit value is set to 1 making the total count of
* 1's in the whole set (including the parity bit)
* an odd number.
* If the count of bits with a value of 1 is odd,
* the count is already odd so the parity bit's
* value is 0.
*
* https://en.wikipedia.org/wiki/Parity_bit
* https://www.hidglobal.com/sites/default/files/hid-understanding_card_data_formats-wp-en.pdf
* http://www.ccdesignworks.com/wiegand_calc.htm
*
* int bin 1count even |P odd |P
* 0 0000000 0 0000000|0 0000000|1
* 81 1010001 3 1010001|1 1010001|0
* 105 1101001 4 1101001|0 1101001|1
* 127 1111111 7 1111111|1 1111111|0
*
* @param {String} bin Binary number
* @param {String} [parity='e'] e:Even o:Odd
* @return {String}
*/
function parityBit(bin='', parity = 'e'){
function bitCount(bin){
return (bin.match(/1/g) || []).length;
}
let isEven = bitCount(bin) % 2 === 0;
if(parity.match(/^e$|^even$/i)) return isEven ? '0' : '1';
return isEven ? '1' : '0';
}
console.assert(parityBit('0000000', 'e') === '0', 'Parity bit must be 0');
console.assert(parityBit('0000000', 'o') === '1', 'Parity bit must be 1');
console.assert(parityBit('1010001', 'e') === '1', 'Parity bit must be 1');
console.assert(parityBit('1010001', 'o') === '0', 'Parity bit must be 0');
console.assert(parityBit('1101001', 'e') === '0', 'Parity bit must be 0');
console.assert(parityBit('1101001', 'o') === '1', 'Parity bit must be 1');
console.assert(parityBit('1111111', 'e') === '1', 'Parity bit must be 1');
console.assert(parityBit('1111111', 'o') === '0', 'Parity bit must be 0');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment