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 8303947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:40:18+00:00 2026-06-08T17:40:18+00:00

I’m currently trying to build a lookup table for a huffman tree using a

  • 0

I’m currently trying to build a lookup table for a huffman tree using a pretty simple preorder traversal algorithm, but I’m getting stuck carrying out very basic bit wise operations. The psuedo code follows:

void preOrder(huffNode *node, int bit) //not sure how to represent bit
{
  if (node == NULL)
    return;

  (1) bit = bit + 0; //I basically want to add a 0 onto this number (01 would go to 010)
  preOrder(node->getLeft(), bit);
  (2) bit = bit - 0 + 1; //This should subtract the last 0 and add a 1 (010 would go to 011)
  preOrder(node->getRight());


}

I’m getting quite confused about how to carry out the operations defined on lines (1) and (2)

What data type type does one use to represent and print binary numbers? In the above example I have the number represented as an int, but i’m pretty sure that that is incorrect. Also how do you add or subtract values? I understand how & and | types logic works, but I’m getting confused as to how one carries out these sorts of operations in code.

Could anyone post some very simple examples?

  • 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-08T17:40:19+00:00Added an answer on June 8, 2026 at 5:40 pm

    Here’s some basic examples of binary operations. I’ve used mostly in-place operations here.

    int bit = 0x02;   //               0010
    bit |= 1;         // OR  0001 ->   0011
    bit ^= 1;         // XOR 0001 ->   0010
    bit ^= 7;         // XOR 0111 ->   0101
    bit &= 14;        // AND 1110 ->   0100
    bit <<= 1;        // LSHIFT 1 ->   1000
    bit >>= 2;        // RSHIFT 2 ->   0010
    bit = ~bit;       // COMPLEMENT -> 1101
    

    If you want to print a binary number you need to do it yourself… Here’s one slightly inefficient, but moderately readable, way to do it:

    char bitstr[33] = {0};
    for( int b = 0; b < 32; b++ ) {
        if( bit & (1 << (31-b)) )
            bitstr[b] = '1';
        else
            bitstr[b] = '0';
    }
    printf( "%s\n", bitstr );
    

    [edit] If I wanted faster code, I might pre-generate (or hardcode) a lookup table with the 8-bit sequences for all numbers from 0-255.

    // This turns a 32-bit integer into a binary string.
    char lookup[256][9] = {
        "00000000",
        "00000001",
        "00000010",
        "00000011",
        // ... etc (you don't want to do this by hand)
        "11111111"
    };
    
    char * lolo = lookup[val & 0xff];
    char * lohi = lookup[(val>>8) & 0xff];
    char * hilo = lookup[(val>>16) & 0xff];
    char * hihi = lookup[(val>>24) & 0xff];
    
    // This part is maybe a bit lazy =)
    char bitstr[33];
    sprintf( "%s%s%s%s", hihi, hilo, lohi, lolo );
    

    Instead, you could do this:

    char *bits = bitstr;
    while( *hihi ) *bits++ = *hihi++;
    while( *hilo ) *bits++ = *hilo++;
    while( *lohi ) *bits++ = *lohi++;
    while( *lolo ) *bits++ = *lolo++;
    *bits = 0;
    

    Or just unroll the whole thing. 😉

    char bitstr[33] = {
        hihi[0], hihi[1], hihi[2], hihi[3], hihi[4], hihi[5], hihi[6], hihi[7],
        hilo[0], hilo[1], hilo[2], hilo[3], hilo[4], hilo[5], hilo[6], hilo[7],
        lohi[0], lohi[1], lohi[2], lohi[3], lohi[4], lohi[5], lohi[6], lohi[7],
        lolo[0], lolo[1], lolo[2], lolo[3], lolo[4], lolo[5], lolo[6], lolo[7],
        0 };
    

    Of course, those 8 bytes in the lookup are the same length as a 64-bit integer… So what about this? Much faster than all that pointless meandering through character arrays.

    char bitstr[33];
    __int64 * intbits = (__int64*)bitstr;
    intbits[0] = *(__int64*)lookup[(val >> 24) & 0xff];
    intbits[1] = *(__int64*)lookup[(val >> 16) & 0xff];
    intbits[2] = *(__int64*)lookup[(val >> 8) & 0xff];
    intbits[3] = *(__int64*)lookup[val & 0xff];
    bitstr[32] = 0;
    

    Naturally, in the above code you would represent your lookup values as int64 instead of strings.

    Anyway, just pointing out that you can write it however is appropriate for your purposes. If you need to optimize, things get fun, but for most practical applications such optimizations are negligible or pointless.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
We're building an app, our first using Rails 3, and we're having to build
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.