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

  • SEARCH
  • Home
  • 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 8439823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:06:36+00:00 2026-06-10T08:06:36+00:00

I need to convert arbitary length binary into an exact ternary representation. Ideally, given

  • 0

I need to convert arbitary length binary into an exact ternary representation. Ideally, given an array of bits char buffer[n], the algorithm would be able to produce a array of trits(analog of bits), and vice versa. Is there such an algorithm?

I am aware of ways to convert individual int to ternary:

int nth_trit(int num, int n)
{
    for(int i = 0; i < n; i++)
        num /= 3;

    return num % 3;
}

Alas, with a bitstream even a long long long int wouldn’t suffice. I think using a big integer library would suffice, although I’m not sure, and feel that there should be better way calculate the ternary representation.

A visual example:

// Conversion is simple(short stream)
Binary  - 0 1 0 0 1 0 0 1
Decimal -             7 3
Ternary -         2 2 0 1

// Conversion is hard(long stream)
Binary  - 1 0 1 0 0 0 0 1 ..........
Ternary - ? ? ?

The short stream is simple because, since it nicely fits into an int, the nth_trit function can be used, but the long stream doesn’t, and so apart from using a big integer library, no easy solution occurs to me.

  • 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-06-10T08:06:38+00:00Added an answer on June 10, 2026 at 8:06 am

    Your algorithm is not so good if the bit buffer is long because each output trit repeats all the divisions also needed for smaller values of n. So converting this algorithm to “bignum” arithmetic will not be what you want.

    Another approach: scanning the bits left to right, each new one updates the previous value:

    val = val * 2 + bit
    

    A trinary number with n trits t[i] has the value

    sum(i = 0 .. n-1) t[i] * 3^i
    

    So a trinary representation of val updated for a new scanned bit becomes,

    [ 2 * sum(i = 0 .. n-1) t[i] * 3^i ] + bit
        = bit + sum(i = 0 .. n-1) 2 * t[i] * 3^i 
        = 2 * t[0] + b + sum(i = 1 .. n) 2 * t[i] * 3^i
    

    To make the code simple let’s compute the trits in an array of unsigned chars. After they’re done you can repack them any way you like.

    #include <stdio.h>
    
    // Compute the trit representation of the bits in the given
    // byte buffer.  The highest order byte is bytes[0].  The
    // lowest order trit in the output is trits[0].  This is 
    // not a very efficient algorithm, but it doesn't use any
    // division.  If the output buffer is too small, high order
    // trits are lost.
    void to_trits(unsigned char *bytes, int n_bytes, 
                  unsigned char *trits, int n_trits)
    {
      int i_trit, i_byte, mask;
    
      for (i_trit = 0; i_trit < n_trits; i_trit++)
        trits[i_trit] = 0;
    
      // Scan bits left to right.
      for (i_byte = 0; i_byte < n_bytes; i_byte++) {
    
        unsigned char byte = bytes[i_byte];
    
        for (mask = 0x80; mask; mask >>= 1) {
          // Compute the next bit.
          int bit = (byte & mask) != 0;
    
          // Update the trit representation
          trits[0] = trits[0] * 2 + bit;
          for (i_trit = 1; i_trit < n_trits; i_trit++) {
            trits[i_trit] *= 2;
            if (trits[i_trit - 1] > 2) {
              trits[i_trit - 1] -= 3;
              trits[i_trit]++;
            }
          }
        }
      }
    }
    
    // This test uses 64-bit quantities, but the trit 
    // converter will work for buffers of any size.
    int main(void)
    {
      int i;
    
      // Make a byte buffer for an easy to recognize value.
      #define N_BYTES 7
      unsigned char bytes [N_BYTES] = 
        { 0xab, 0xcd, 0xef, 0xff, 0xfe, 0xdc, 0xba };
    
      // Make a trit buffer.  A 64 bit quantity may need up to 42 trits.
      #define N_TRITS 42
      unsigned char trits [N_TRITS];
    
      to_trits(bytes, N_BYTES, trits, N_TRITS);
    
      unsigned long long val = 0;
      for (i = N_TRITS - 1; i >= 0; i--) {
        printf("%d", trits[i]);
        val = val * 3 + trits[i];
      }
      // Should prinet value in original byte buffer.
      printf("\n%llx\n", val);
    
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to convert a time difference (diff) between two actions into a human
I need to convert a Joda-Time DateTime into a String, in the following format:
I need to convert a column data into single row with multiple columns Example
I need to convert hierarchical data (AVRO data, which boils down to JSON) into
I need to convert values of type T into JsArray . For example, I
I need to convert an array of bytes to another base, namely 85. In
I need convert some Mac Numbers(00163e2fbab7) into Mac String. (with :) Is there some
I need to convert a StringBuffer Array to String Array for sorting. Is there
I need to convert an arbitrary amount of milliseconds into Days, Hours, Minutes Second.
I need convert this array data = [ #SHO 0x56, 0x0d, #CMD 0x1, 0x00,

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.