Termite Posted June 9, 2015 Report Share Posted June 9, 2015 ola gostaria de saber se é possível modificar,ou mapear um tipo primitivo como char,int,ou float atravez de um ponteiro de struct que tenha um campo de bits dentro? Quote Link to comment Share on other sites More sharing options...
0 britivaldo Posted July 2, 2015 Report Share Posted July 2, 2015 (edited) Sim! Independentemente da arquitetura, os sistemas atuais, tem um inteiro CHAR de 8 bits. Se mapearmos um a um em estrutura, e aplicamos cast é possível manipula-los individualmente. Veja como: /** @authr Mauro Britivaldo * License all free on edition and published */ #include <stdio.h> //MANUAL TRACE typedef struct byte byte; //Definido a nossa estrutura struct byte { //Mapeamos Bit a Bit com campos da mesma unsigned char b0: 1; unsigned char b1: 1; unsigned char b2: 1; unsigned char b3: 1; unsigned char b4: 1; unsigned char b5: 1; unsigned char b6: 1; unsigned char b7: 1; }; int main (void) { // char c_caracter; //Declaramos uma variável inteira byte *bt; //Declaramos um ponteiro da estrutura bt = (byte*) &c_caracter; bt->b4 = 1; //Manipulamos em baixo nível; bit a bit := |16| //Observe o resultado // printf ("Before: Show byte: %d\n", c_caracter); bt->b4 = 0; printf ("After: Show byte: %d\n", c_caracter); return 0; } Edited July 2, 2015 by britivaldo Quote Link to comment Share on other sites More sharing options...
0 Termite Posted July 5, 2015 Author Report Share Posted July 5, 2015 valeu cara Quote Link to comment Share on other sites More sharing options...
Question
Termite
ola gostaria de saber se é possível modificar,ou mapear um tipo primitivo como char,int,ou float atravez de um ponteiro de struct que tenha um campo de bits dentro?
Link to comment
Share on other sites
2 answers to this question
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.