Thread: a 1 bit variable

  1. #1
    Registered User
    Join Date
    Jan 2021
    Posts
    30

    a 1 bit variable

    are there any 1 bit variables in C?

    for example if int is 4 bits(I think)
    Last edited by oods1; 01-12-2021 at 01:10 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    are there any 1 bit variables in C?
    No.

    for example if int is 4 bits(I think)
    No, char, short, long, are in bytes, a byte is 8 bits.

  3. #3
    Registered User
    Join Date
    Jan 2021
    Posts
    30
    Quote Originally Posted by jimblumberg View Post
    No.


    No, char, short, long, are in bytes, a byte is 8 bits.
    Damn, thanks anyway=]

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    This sounds like an XY problem. What would you need a 1-bit variable for?

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by oods1 View Post
    are there any 1 bit variables in C?

    for example if int is 4 bits(I think)
    Do you mean one bit or one byte? Most computers address memory in 8 bit bytes. In C, and int is usually 4 bytes, or 32 bits.

    A boolean variable is logically one bit, either on or off. However C doesn't support this as bits are not directly addressable,
    declaring a pointer to a one bit variable would lead to a lot of complexity for little advantage.

    So in C booleans are wider types, and any non-zero value is considered true.

    In C, a "char" is one byte, usually 8 bits. That's the smallest addressable unit of memory.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  6. #6
    Registered User
    Join Date
    Apr 2019
    Posts
    62
    I think you're a bit confused. The smallest variable possible is the smallest addressable value by the system. On most systems this is 1 byte, not 1 bit. A char is 1 byte wide and is the smallest type available.

    However, there are struct bit fields. Bit fields are used to pack data into a struct as tightly as possible. For example, you could have a struct like this.

    Code:
    struct flags {
      unsigned active:1;
      unsigned alive:1;
      unsigned has_fuel:1;
      unsigned flying:1;
    };
    Each of these fields will only take 1 bit of memory and the C compiler will pack them together in the same byte. However, this is a bit misleading. You cannot take the address of any of these bit fields because you cannot address a single bit. Behind the scene the C compiler and just setting or clearing single bits by taking a larger value from memory, toggling and single bit and replacing the entire value. Bit fields blur the line a bit of what is and is not a "variable."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-13-2020, 04:27 AM
  2. Replies: 2
    Last Post: 05-04-2017, 11:07 PM
  3. Replies: 7
    Last Post: 04-16-2011, 12:28 PM
  4. Replies: 8
    Last Post: 02-14-2010, 04:14 PM
  5. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM

Tags for this Thread