Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3492190
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:43:42+00:00 2026-05-18T11:43:42+00:00

Let me start with some background: By tribool I understand a variable which can

  • 0

Let me start with some background:

By “tribool” I understand a variable which can hold one of the following values: true, false or null.

In question Copying array of ints vs pointers to bools , the OP wanted to have an array of tribools (more or less) which would be as small as possible.

With “a bit of” most basic bit-fu I came up a solution which used 2 bits per tribool and allowed to store the OP’s array of 64 tribools in 16 bytes, which is OK.

The tribool mechanics I used were simple, like:

  • boolean A means “null or not null”,
  • boolean B means “true or false if not null”.

But then I thought… An algorithmical definition of a “bit” is:

A bit is the amount of information which specifies which of two equally probable events shall occur.

Clearly a true/false value is 1 bit big. Two true-false values as a whole are 2 bit big.

And what about our conceptual tribool?

My point is: In terms of the size of contained information, a tribool is bigger than 1 bit but smaller than 2 bits.

  • Justification 1: Assume we implement our if boolean as described above. If boolean A is “null”, the value of boolean B is redundant and doesn’t carry any relevant information.
  • Justification 2: It’s impossible to store information from 2 independent boolean values in one tribool, so it has

(None of the above is a formal proof, but I believe that we can agree that about the “size” of the tribool being strictly bigger than 1 bit and strictly smaller than 2.)


My question is:

How to programatically take advantage of the fact that a tribool has less information than 2 bits, and implement in software (c, c++?) an array of N tribools which would have the memory footprint smaller than N/4 bytes for some N?

Yes, I do understand that such an implementation isn’t really hardware-friendly and would perform slower than any common solution with redundance (as those presented in the OP’s question). Let’s just optimize for space, not for efficiency.

Clearly this implementation needs a different representation of a tribool than a pair of bools (which is by itself redundant, as described before). The theory says it’s possible to achieve that goal and I like to see an actual implementation. Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T11:43:43+00:00Added an answer on May 18, 2026 at 11:43 am

    Your intuition is correct, this is certainly possible. This is basically a form of arithmetic coding, or at least a simple instance of it.

    The easiest way to think of it is to imagine encoding your array of “tribools” as a number in base 3 – e.g. 0=FALSE, 1=TRUE, 2=NULL. Then the following array:

    {TRUE, FALSE, NULL, NULL, FALSE, FALSE, TRUE}
    

    encodes to the number

    1022001
    

    which you can then convert to decimal in the normal way:

    (1*3^0)+(0*3^1)+(0*3^2)+(2*3^3)+(2*3^4)+(0*3^5)+(1*3^6) = 946
    

    Each tribool takes up ln(3)/ln(2) bits (about 1.58), so using this method you can store 20 tribools in 32 bits – so you can store an N=20 array in 4 bytes (where N/4 is 5).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First, let me start with some background: I have a web service which accepts
So, let's start with some background [modified to make more concrete[. I have realised
Every One Expert Here is some of My Problem to how i can start
Let's say I do the following (foo starts out as some object): [foo release];
Let me give you some background on my scenario. I got a multi language
Can some please suggest an alternative to using two different handlers in the one
For some reason a background thread in my app can't change any labels, textbox
Let me start with the questions, and then fill in the reasons/background. Question: Are
Let's start with the following snippet: Foreach(Record item in RecordList){ .. item = UpdateRecord(item,
Let's start with the Hello World program detailed at http://developer.android.com/resources/tutorials/hello-world.html . Let's add the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.