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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:16:26+00:00 2026-05-18T05:16:26+00:00

I want to create a matrix representation for a graph algorithm using the least

  • 0

I want to create a matrix representation for a graph algorithm using the least memory possible.

So I decided to try using a bit representation of the values of the matrix, but I also know that doing this by C is (AFAIK) impossible, because a bit is not addressable.

Then I read a post here suggesting to use a struct that can help me do so by using, e.g., an int (4 bytes, so 32-bit) and, with some magic and bitshifts, use it as an “array” of bits.

Got that, but I can’t really realize how exactly I could do this. I got confused…

I’m thinking about using a structure to store a int/void pointer to n bytes corresponding to the least number of bytes to the ‘n’ number of bits allocated and the ‘k’ number of bits in that representation, something such as that.

So I thought you could help me realize what’s the best approach for this kind of solution.

Note: Why I’m so confused? I’m still graduating in Computer Science and I just started to study graphs. Also just finished a laboratory project on that (implemented it as a matrix but used some mathemagic to alloc only half of the matrix and represented it as symectrical), but I’m trying to extend the matter. Also because I got extremely curious 🙂

Thanks all.

P.S.: almost forgot, I’m programming this in C, but I can understand C++, .Net languages and Java very well. Thanks again.

  • 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-18T05:16:26+00:00Added an answer on May 18, 2026 at 5:16 am

    There are a couple of tricky bits here: working on individual bits within a large array; and simulating a 2-dimensional array with a 1-dimensional one. It’s best to solve these separately at first.

    Start with some helper functions that let you work on individual bits. Something like:

    typedef unsigned int BYTE;  /* Int type to use for data. */
    #define BYTE_SIZE (sizeof(BYTE)*8)   /* How many bits in each one. */
    
    void set_bit(BYTE *data, int pos, int value)
    {
        int index = pos / BYTE_SIZE;   /* Which byte to adjust. */
        int offset = pos % BYTE_SIZE;  /* Which bit within it. */
        /* 1 << offset turns into the place value for the bit at offset. */
        /* x | 1 << offset sets the bit there (an OR operation);
           ~(1 << offset) gets something with all bits except that bit set, and
           x & ~(1 << offset) clears the bit with an AND operation on x. */
        if (value)
            data[index] = data[index] | (1 << offset);
        else
            data[index] = data[index] & ~(1 << offset);
    }
    
    int test_bit(int *data
    {
        int index = pos / BYTE_SIZE;
        int offset = pos % BYTE_SIZE;
        /* An AND operation to see if the bit is set, then compare against 0
           so that 1 or 0 is returned instead of the place value. */
        return (data[index] & (1 << offset)) != 0;
    }
    

    You then move up a level with a structure to hold the array of bytes, and some data about the dimensions. A 2-dimensional array can be simulated with a 1-d one, by translating a an operation on bit (x,y) to one on bit y*width+x.

    struct BITMATRIX
    {
        BYTE *data;
        int width;
        int height;
    }
    
    void set_bit_matrix(struct BITMATRIX *bm, int x, int y, int value)
    {
        int pos = y * bm->width + x;
        set_bit(bm->data, pos, value);
    }
    
    /* Etc. */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a Java application bundle for Mac without using Mac. According
I want to create an allocator which provides memory with the following attributes: cannot
I want create texture atlas(matrix of images) from multiple images.Is their any applescript that
I want to create a current,30, 60, 90, day aging reports in Matrix Format
vector= 3 4 8 5 2 1 6 the matrix i want to create
I want create a drop shadow around the canvas component in flex. Technically speaking
I want to create a client side mail creator web page. I know the
I want to create a function that performs a function passed by parameter on
I want to create a simple http proxy server that does some very basic
I want to create a draggable and resizable window in JavaScript for cross browser

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.