Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

226
Views
How do I create a 3 bit variable as datatype in C?

I can typedef char to CHAR1 which is 8 bits. But how can I make 3 bit variable as datatype?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You might want to do something similar to the following:

struct
{
  .
  .
  .
  unsigned int fieldof3bits : 3;
  .
  .
  .
} newdatatypename;

In this case, the fieldof3bits takes up 3 bits in the structure (based upon how you define everything else, the size of structure might vary though).

This usage is something called a bit field.

From Wikipedia:

A bit field is a term used in computer programming to store multiple, logical, neighboring bits, where each of the sets of bits, and single bits can be addressed. A bit field is most commonly used to represent integral types of known, fixed bit-width.

over 4 years ago · Santiago Trujillo Report

0

It seems you're asking for bitfields https://en.wikipedia.org/wiki/Bit_field Just be aware that for some cases it's can be safer just use char or unsigned char instead of bits (compiler specific, physical memory layout etc.)

Happy coding!

over 4 years ago · Santiago Trujillo Report

0

typedef struct {

int a:3;
}hello; 

It is only possible when it is inside structure else it is not

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!