Jump to content
Fórum Script Brasil
  • 0

(Resolvido) campos de bits



2 answers to this question

Recommended Posts

  • 0


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 by britivaldo
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Forum Statistics

    • Total Topics
    • Total Posts
  • Create New...