-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathop_bits.c
51 lines (40 loc) · 1.02 KB
/
op_bits.c
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
#include "op_bits.h"
int tailleFichier(FILE* fp)
{
int res = 0;
fseek(fp, 0, SEEK_END);
res = ftell(fp);
fseek(fp, 0, SEEK_SET);
return res;
}
/* n de 0 à 8*(taille buffer) */
void ecritNiemeBit(unsigned char *buff, unsigned char bit, int n) {
int size_char = sizeof(unsigned char)*8; /* nb bits par int */
int nr = n%size_char, nd = n/size_char;
int masque = 0x1;
masque <<= (size_char -1 -nr);
if (bit) { /* mise à 1 */
buff[nd] = buff[nd] | masque;
} else { /* mise à 0 */
buff[nd] = buff[nd] & (~masque);
}
return;
}
/* n de 0 à 8*(taille buffer) */
/* retour : valeur du nieme bit (0 ou 1) */
unsigned char litNiemeBit(unsigned char *buff, int n) {
int size_char = sizeof(unsigned char)*8; /* nb bits par int */
int nr = n%size_char, nd = n/size_char;
unsigned int masque = 0x1;
masque <<= (size_char -1 -nr);
return (buff[nd] & masque?1:0);
}
void afficher_binaire(unsigned char a)
{
int i;
for(i=CHAR_BIT-1 ; i>=0 ; i--)
{
printf("%c", (a&(1<<i))?'1':'0');
}
printf("\n");
}