Συντονιστής: konnn

byte.valuebyte.bits.bXbyte.value = 2;byte.bits.b1 = true;
byte.bits.b7 = true;
#include <stdio.h>
#include <stdbool.h>
typedef struct bits // structure of 8 bitfields (1 bit per field)
{ // !!!! their order is MACHINE DEPENDENT !!!!
_Bool b1:1;
_Bool b2:1;
_Bool b3:1;
_Bool b4:1;
_Bool b5:1;
_Bool b6:1;
_Bool b7:1;
_Bool b8:1;
} Bits;
typedef union byte { // an unsigned byte represented as union
unsigned char value; // ... the numerical rerpesentation of the byte
Bits bits; // ... the bit representation of the byte
} Byte;
// -------------------------------------------------------------------------------------
// reset all byte bits to 0
//
void byte_resetbits( Byte *byte )
{
byte->bits.b1 = false;
byte->bits.b2 = false;
byte->bits.b3 = false;
byte->bits.b4 = false;
byte->bits.b5 = false;
byte->bits.b6 = false;
byte->bits.b7 = false;
byte->bits.b8 = false;
return;
}
// -------------------------------------------------------------------------------------
// set all byte bits to 1
//
void byte_setbits( Byte *byte )
{
byte->bits.b1 = true;
byte->bits.b2 = true;
byte->bits.b3 = true;
byte->bits.b4 = true;
byte->bits.b5 = true;
byte->bits.b6 = true;
byte->bits.b7 = true;
byte->bits.b8 = true;
return;
}
// -------------------------------------------------------------------------------------
// print all byte bits (the order is machine dependent)
//
void print_bytebits( Byte byte )
{
printf( "%u%u%u%u%u%u%u%u",
byte.bits.b1,
byte.bits.b2,
byte.bits.b3,
byte.bits.b4,
byte.bits.b5,
byte.bits.b6,
byte.bits.b7,
byte.bits.b8
);
return;
}
// -------------------------------------------------------------------------------------
void print_byte( Byte byte )
{
printf("byte value : %u\n", byte.value );
printf("byte bits : " );
print_bytebits( byte );
puts("\n");
return;
}
// -------------------------------------------------------------------------------------
int main( void )
{
Byte byte;
byte.value = 1; // assign decimal value of 1 to byte
print_byte( byte );
byte.value = 2; // assign decimal value of 2 to byte
print_byte( byte );
byte_resetbits( &byte ); // reset all byte bits to 0
print_byte( byte );
byte_setbits( &byte ); // set all byte bits to 1
print_byte( byte );
return 0;
}
byte value : 1
byte bits : 10000000
byte value : 2
byte bits : 01000000
byte value : 0
byte bits : 00000000
byte value : 255
byte bits : 11111111


stamatiou έγραψε:Εγώ έχω 2 μέχρι τώρα:
1. Το _Bool είναι μεταβλητή μέσα στο stdbool.h;
έγραψε:2. Πώς γίνεται να μετατρέπεται από value σε byte;

migf1 έγραψε:stamatiou έγραψε:Εγώ έχω 2 μέχρι τώρα:
1. Το _Bool είναι μεταβλητή μέσα στο stdbool.h;
Ναι, κι αυτό έχει προστεθεί στην αναθεώρηση C99... μπορείς να το σβήσεις τελείως, να δηλώσεις τα bitfields ως unsigned int ή ως unsigned char αντί για _Bool και όταν τους αναθέτεις τιμές χειροκίνητα, αντί για true/false να τους βάζεις 1/0.έγραψε:2. Πώς γίνεται να μετατρέπεται από value σε byte;
Τι εννοείς; Για εξήγησέ μου καλύτερα.


migf1 έγραψε:Επειδή είναι union, αυτός είναι ο ρόλος του union.

#include <stdio.h>
typedef union manytypes {
long int longtype;
int intype;
char chartype;
} ManyTypes;
// -------------------------------------------------------------------------------------------------------
int main( void )
{
ManyTypes var;
var.inttype = 65;
...
}


typedef union byte { // an unsigned byte represented as union
unsigned char value; // ... the numerical rerpesentation of the byte
Bits bits; // ... the bit representation of the byte
} Byte;
