Post by pehacheJe ne suis pas vraiment très habitué au C, je ne fais que bricoler...
J'en ai fait industriellement pendant 10 ans, récemment beaucoup moins.
Post by pehachePar curiosité tu écrirais ça comme ça ?
long double x;
char* cx;
cx = (char*)&x;
et ensuite accéder aux différents octect par c[i] ?
Le principe est ainsi oui.
Voici le code pour un bitset générique avec des octets et un exemple
avec un float. Pas bien testé!
https://login.alphanet.ch/~schaefer/tmp/bitset.c
chmod a+rx bitset.c
Avec des macros ça pourrait être plus rapide.
Voici la sortie. L'assertion ratée à la fin c'est normal, on demande un
bit non représentable.
$ ./bitset.c
here a long double is: 16 bytes.
trying with: 0
byte 0 bit 0
byte 0 bit 0
bit value: 1
bytes: 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00.
byte 0 bit 0
byte 0 bit 0
bit value: 0
bytes: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00.
trying with: 1
byte 0 bit 1
byte 0 bit 1
bit value: 1
bytes: 0x02 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00.
byte 0 bit 1
byte 0 bit 1
bit value: 0
bytes: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00.
trying with: 2
byte 0 bit 2
byte 0 bit 2
bit value: 1
bytes: 0x04 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00.
byte 0 bit 2
byte 0 bit 2
bit value: 0
bytes: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00.
trying with: 3
byte 0 bit 3
byte 0 bit 3
bit value: 1
bytes: 0x08 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00.
byte 0 bit 3
byte 0 bit 3
bit value: 0
bytes: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00.
trying with: 4
byte 0 bit 4
byte 0 bit 4
bit value: 1
bytes: 0x10 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00.
byte 0 bit 4
byte 0 bit 4
bit value: 0
bytes: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00.
trying with: 5
byte 0 bit 5
byte 0 bit 5
bit value: 1
bytes: 0x20 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00.
byte 0 bit 5
byte 0 bit 5
trying with: 6
byte 0 bit 6
byte 0 bit 6
bit value: 1
bytes: 0x40 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00.
byte 0 bit 6
byte 0 bit 6
bit value: 0
bytes: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00.
trying with: 7
byte 0 bit 7
byte 0 bit 7
bit value: 1
bytes: 0x80 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00.
byte 0 bit 7
byte 0 bit 7
bit value: 0
bytes: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00.
trying with: 8
byte 1 bit 0
byte 1 bit 0
bit value: 1
bytes: 0x00 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00.
byte 1 bit 0
byte 1 bit 0
bit value: 0
bytes: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00.
trying with: 15
byte 1 bit 7
byte 1 bit 7
bit value: 1
bytes: 0x00 0x80 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00.
byte 1 bit 7
byte 1 bit 7
bit value: 0
bytes: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00.
trying with: 16
byte 2 bit 0
byte 2 bit 0
bit value: 1
bytes: 0x00 0x00 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00.
byte 2 bit 0
byte 2 bit 0
bit value: 0
bytes: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00.
trying with: 42
byte 5 bit 2
byte 5 bit 2
bit value: 1
bytes: 0x00 0x00 0x00 0x00 0x00 0x04 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00.
byte 5 bit 2
byte 5 bit 2
bit value: 0
bytes: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00.
trying with: 24
byte 3 bit 0
byte 3 bit 0
bit value: 1
bytes: 0x00 0x00 0x00 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00.
byte 3 bit 0
byte 3 bit 0
bit value: 0
bytes: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00.
trying with: 127
byte 15 bit 7
byte 15 bit 7
bit value: 1
bytes: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x80.
byte 15 bit 7
byte 15 bit 7
bit value: 0
bytes: 0x00 0x00 0x00 0x00 0x00 0x00 0xbitset: ./bitset.c:59: bit_assert: Assertion `bit_set(bit_number, value, bytes, size)' failed.
./bitset.c: line 3: 5242 Aborted ./$(basename $0 .c)