0

I've created a 64*64 size hadamard matrix of characters and would now like to pass the binary values of each row of the matrix (i.e. a 64 bit binary number) into an array (lookup). The code to do this is the double for loop at the bottom of the main function.

Using the code here when I execute a list of ones are printed, instead of the full binary value.

#include <stdio.h>
#include <stdlib.h>

void create_hadamard_matrix(char** h, int N)
{
    int ind,indr,indc;

    for (ind=1;ind<=N;ind*=2)
    {

        // find the half length of H matrix for this iteration
        int sideHalf=ind/2;

        //for n=1 H matrix
        if (ind==1)
        {
            h[0][0]='1';
        }
        else
        {
            //put in values in bottom left of new H matrix
            for (indr=0; indr<sideHalf; indr++)
            {
                for (indc=0; indc<sideHalf; indc++)
                {
                    h[indr+sideHalf][indc]=h[indr][indc];
                }
            }
            //put in values in top right of new H matrix
            for (indr=0; indr<sideHalf; indr++)
            {
                for (indc=0; indc<sideHalf; indc++)
                {
                    h[indr][indc+sideHalf]=h[indr][indc];
                }
            }
            //put in values in bottom right of new H matrix
            for (indr=0; indr<sideHalf; indr++)
            {
                for (indc=0; indc<sideHalf; indc++)
                {
                    //invert characters
                    if (h[indr][indc]=='1')
                    {
                        h[indr+sideHalf][indc+sideHalf]='0';
                    }
                    else
                    {
                        h[indr+sideHalf][indc+sideHalf]='1';
                    }

                }
            }
        }
    }
}


void main()
{
    int i,j,N=64;

    char **h = (char**) malloc(N  * sizeof(char*));
    for ( i = 0; i < N; i++ )
    {
        h[i] = (char*) malloc(N  * sizeof(char));
    }

    create_hadamard_matrix(h, N);

    unsigned long long *lookup = (unsigned long long*) malloc(N * sizeof(unsigned long long));

    for (i=0; i<N; i++)
    {
        for (j=0; j<N; j++)
        {
            if (h[i][j]=='1')
            {
                lookup[i]=(1<<(63-j));
            }   

        }
        printf("%llu",lookup[i]);   
        printf("\n");
    }
}
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • For N = 64, you'd do better to write `unsigned long long lookup[N];` instead of using `malloc()`. Ideally (though it won't really matter in this code), you should be able to point to the call to `free()` that releases the memory allocated by each call to `malloc()` et al. – Jonathan Leffler Sep 04 '13 at 06:05

1 Answers1

5

In main() (which should return an int, btw), change this:

if (h[i][j]='1');

To this:

if (h[i][j]=='1')

for hopefully obvious reasons.


Next, your lookup array is not being initialized after allocation (and though you may not think it need be, it is required for the next error fix after this to work):

Change this:

unsigned long long *lookup = (unsigned long long*) malloc(N * sizeof(unsigned long long));

To this:

unsigned long long *lookup = calloc(N, sizeof(*lookup));

which will zero-fill the lookup array on inception. You could also memset if you wanted to. The choice is yours.


And next, you're overwriting your lookup value for each row for multiple bits. I.e. the last bit you write into any row's lookup entry is the only one that is saved. You need to accumulate them using |= rather than just =. Also, you need to ensure the shift-operation is done on a ulonglong. As written it is shifting on an int well-beyond the int width. See below.

Change this:

lookup[i]=(1<<(63-j));

To this:

lookup[i]|=(((unsigned long long)1)<<(63-j));

Putting that all together and using a bit-printer I just slammed out, this is what I get:

1111111111111111111111111111111111111111111111111111111111111111
1010101010101010101010101010101010101010101010101010101010101010
1100110011001100110011001100110011001100110011001100110011001100
1001100110011001100110011001100110011001100110011001100110011001
1111000011110000111100001111000011110000111100001111000011110000
1010010110100101101001011010010110100101101001011010010110100101
1100001111000011110000111100001111000011110000111100001111000011
1001011010010110100101101001011010010110100101101001011010010110
1111111100000000111111110000000011111111000000001111111100000000
1010101001010101101010100101010110101010010101011010101001010101
1100110000110011110011000011001111001100001100111100110000110011
1001100101100110100110010110011010011001011001101001100101100110
1111000000001111111100000000111111110000000011111111000000001111
1010010101011010101001010101101010100101010110101010010101011010
1100001100111100110000110011110011000011001111001100001100111100
1001011001101001100101100110100110010110011010011001011001101001
1111111111111111000000000000000011111111111111110000000000000000
1010101010101010010101010101010110101010101010100101010101010101
1100110011001100001100110011001111001100110011000011001100110011
1001100110011001011001100110011010011001100110010110011001100110
1111000011110000000011110000111111110000111100000000111100001111
1010010110100101010110100101101010100101101001010101101001011010
1100001111000011001111000011110011000011110000110011110000111100
1001011010010110011010010110100110010110100101100110100101101001
1111111100000000000000001111111111111111000000000000000011111111
1010101001010101010101011010101010101010010101010101010110101010
1100110000110011001100111100110011001100001100110011001111001100
1001100101100110011001101001100110011001011001100110011010011001
1111000000001111000011111111000011110000000011110000111111110000
1010010101011010010110101010010110100101010110100101101010100101
1100001100111100001111001100001111000011001111000011110011000011
1001011001101001011010011001011010010110011010010110100110010110
1111111111111111111111111111111100000000000000000000000000000000
1010101010101010101010101010101001010101010101010101010101010101
1100110011001100110011001100110000110011001100110011001100110011
1001100110011001100110011001100101100110011001100110011001100110
1111000011110000111100001111000000001111000011110000111100001111
1010010110100101101001011010010101011010010110100101101001011010
1100001111000011110000111100001100111100001111000011110000111100
1001011010010110100101101001011001101001011010010110100101101001
1111111100000000111111110000000000000000111111110000000011111111
1010101001010101101010100101010101010101101010100101010110101010
1100110000110011110011000011001100110011110011000011001111001100
1001100101100110100110010110011001100110100110010110011010011001
1111000000001111111100000000111100001111111100000000111111110000
1010010101011010101001010101101001011010101001010101101010100101
1100001100111100110000110011110000111100110000110011110011000011
1001011001101001100101100110100101101001100101100110100110010110
1111111111111111000000000000000000000000000000001111111111111111
1010101010101010010101010101010101010101010101011010101010101010
1100110011001100001100110011001100110011001100111100110011001100
1001100110011001011001100110011001100110011001101001100110011001
1111000011110000000011110000111100001111000011111111000011110000
1010010110100101010110100101101001011010010110101010010110100101
1100001111000011001111000011110000111100001111001100001111000011
1001011010010110011010010110100101101001011010011001011010010110
1111111100000000000000001111111100000000111111111111111100000000
1010101001010101010101011010101001010101101010101010101001010101
1100110000110011001100111100110000110011110011001100110000110011
1001100101100110011001101001100101100110100110011001100101100110
1111000000001111000011111111000000001111111100001111000000001111
1010010101011010010110101010010101011010101001011010010101011010
1100001100111100001111001100001100111100110000111100001100111100
1001011001101001011010011001011001101001100101101001011001101001

Hopefully that is what its supposed to look like (honestly I haven't a clue).

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • 1
    Remove the semicolon too. – kaisernahid Sep 04 '13 at 06:01
  • I make the changes, the list is still mostly ones, but with a few other numbers interspersed in between (i.e. 1 2 4 1 16 etc). I was hoping to get a 64 bit binary number, any idea why it's not working? –  Sep 04 '13 at 06:16
  • What is it you want? You mean something like 101101001010... etc? You'll have to write a translation function for that. `printf()` won't do it natively. [See this question](http://stackoverflow.com/questions/111928/is-there-a-printf-converter-to-print-in-binary-format). – WhozCraig Sep 04 '13 at 06:25
  • Ok, I didn't know that, ill fix that bit later. However the numbers are still incorrect, i.e. the top row of the h matrix is all ones so the decimal should be 295147905179352825840, but it prints as just 1. –  Sep 04 '13 at 06:32
  • @Bob Sorry, but isn't a fully lit 64-bit unsigned 18446744073709551615 ? (i.e. 0xFFFFFFFFFFFFFFFF) ? (and I think I know what the problem is regardless) – WhozCraig Sep 04 '13 at 06:47
  • Yes it is, I put a zero onto the end of the number in the converter accidentally. What is the problem? –  Sep 04 '13 at 06:49
  • @Bob Read the updated answer. That should spell it out for you. Had a few typos I had to address. Terribly sorry about that. – WhozCraig Sep 04 '13 at 06:53
  • @WhozCraig didn't you ment calloc instead of malloc ? – gastush Sep 04 '13 at 06:58
  • Thanks, I didn't see the updated answer. When I replaced The matrix allocation with the suggested code I got the error 'too many arguments to function malloc', however it worked with the original setup. –  Sep 04 '13 at 07:02
  • @Bob if it does its by chance. `malloc()` doesn't zero-initialize the allocation content, so you need to either follow it with a `memset()` to zero for the same number of bytes, or just use the `calloc()` call I updated in the answer. But you need to do one or the other. – WhozCraig Sep 04 '13 at 07:03