Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

177
Visualizações
Smallest data type - can I define a one bit variable?

I need only one bit to represent my data - 1 or 0. What is the best way to do so in C? The "normal" data types are too large.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You could create

typedef struct foo
{
    unsigned x:1;
} foo;

Where you have told the compiler that you'll only be using one bit of x.

But due to structure packing arrangements (the C standard is intentionally flexible in order that compilers can optimise according to the machine architecture), it may well turn out that this still occupies as much space in memory as a regular unsigned and an array of foos doesn't have to be bitwise contiguous.

over 4 years ago · Santiago Trujillo Relatório

0

If you really want, you can create a structure with a member variable , bit-fielded to 1 bit.

Remember, the data type of the member variable needs to be unsigned, as you need to store 0 and 1.

over 4 years ago · Santiago Trujillo Relatório

0

If you don't need millions of these flags or have extremely limited memory constraints, the best way is definitively an int.

This is because an int normally corresponds to the natural word size of your platform and can, properly aligned, be accessed quickly. The machine reads a word at a time anyways and using the single bits requires masking and shifting, that costs time. On your typical PC with gigabytes of RAM, this would be just silly.

If memory consumption really is an issue, there are bitfield structures.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda