1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
#include <iostream>
unsigned int GetFirstBitAt1 (unsigned int x);
unsigned int CountBitAt1 (unsigned int x);
#define Count_bit(x) (CountBitAt1 (count_bit[x])) // nb de bits a 1 ds x
#define First_bit(x) (GetFirstBitAt1 (first_bit[x])) // numero du premier bit a 1 ds x
using namespace std;
int main()
{
unsigned char first_bit[]= {1, 4, 0x80};// en binaire: 1, 100, 10000000
unsigned char count_bit[] = {5, 15, 91};// en binaire : 101 , 1111, 1011011
cout << First_bit(0) << endl;//output : 0
cout << First_bit(1) << endl;//output : 2
cout << First_bit(2) << endl;//output : 7
cout << Count_bit(0) << endl;// output : 2
cout << Count_bit(1) << endl;// output : 4
cout << Count_bit(2) << endl;// output : 5
return 0;
}
unsigned int GetFirstBitAt1 (unsigned int x)
{
unsigned int temp;
__asm{
bsf eax,x;
jz zero;
jmp end;
zero:
xor eax,eax;
end:
mov temp, eax;
}
return temp;
}
unsigned int CountBitAt1 (unsigned int x)
{
unsigned int temp;
_asm{
xor ecx,ecx;//compteur de boucle
xor edx,edx;//compteur de bits
movzx eax,x;
boucle:
bt eax,ecx;
jnc nothing;
inc edx;
nothing:
inc ecx;
cmp ecx,16; //nombre de bits pour un UCHAR (de 0 à 15)
jb boucle;
mov temp,edx;
}
return temp;
} |
Partager