04-01-2021, 09:47 AM
To bas rijetko zatreba, ali ipak zatreba... Datum na ovom fajlu, koji iskopah sa svog diska, je iz 2004. god.
Ovo je najbrzi nacin za brojanje bitova, a kad se izbroje, samo treba setovati taj parity bit, na osnovu rezultata tog brojanja, tj. njegovog bita 0, i sve radi uredno... Imate slanje sa 9 bita i setovanim parity bitom.
================================================
#include <pic.h>
/*
Freely distributable in this original form.
Any questions or comments? shane@keyghost.com
Updates to FAQ available from http://www.keyghost.com/htpic
Bit counter by Ratko Tomic, modified for PIC micro by Shane Tolmie
*/
unsigned char bit_count(unsigned int x)
{
unsigned char n=0;
/*
** The loop will execute once for each bit of x set, this is in average
** twice as fast as the shift/test method.
*/
if (x) do
n++;
while (0!=(x=x & (x-1)));
return (n);
}
unsigned int n;
unsigned char c;
void main(void)
{
n=0;
while (n<0xFFFE)
{
n++; //put a break point here and watch c and n!
printf("n=%d, c=%c\n", n, c);
c=bit_count(n);
}
}
===============================================
Ovo je najbrzi nacin za brojanje bitova, a kad se izbroje, samo treba setovati taj parity bit, na osnovu rezultata tog brojanja, tj. njegovog bita 0, i sve radi uredno... Imate slanje sa 9 bita i setovanim parity bitom.
================================================
#include <pic.h>
/*
Freely distributable in this original form.
Any questions or comments? shane@keyghost.com
Updates to FAQ available from http://www.keyghost.com/htpic
Bit counter by Ratko Tomic, modified for PIC micro by Shane Tolmie
*/
unsigned char bit_count(unsigned int x)
{
unsigned char n=0;
/*
** The loop will execute once for each bit of x set, this is in average
** twice as fast as the shift/test method.
*/
if (x) do
n++;
while (0!=(x=x & (x-1)));
return (n);
}
unsigned int n;
unsigned char c;
void main(void)
{
n=0;
while (n<0xFFFE)
{
n++; //put a break point here and watch c and n!
printf("n=%d, c=%c\n", n, c);
c=bit_count(n);
}
}
===============================================