how to return an integer array from functions

R

rsk

Friends,

The following logic just converts the hexadecimal values to the binay
values taken into the array.

for(i=31;i>=0;i--){
if ((1<<i)&var1)
array_var1=1;
else
array_var1=0;
}

I have to perform the above operation in so many places,so i wanted to
have a function for this.

Can you give some idea how can i do this??


Thanks in advance.
Rsk...
 
R

Richard Heathfield

rsk said:
Friends,

The following logic just converts the hexadecimal values to the binay
values taken into the array.

for(i=31;i>=0;i--){
if ((1<<i)&var1)
array_var1=1;
else
array_var1=0;
}

I have to perform the above operation in so many places,so i wanted to
have a function for this.

Can you give some idea how can i do this??


What you ask for ("how to return an integer array from functions") is
impossible, but what you want to achieve is not impossible, if we
re-state it in a way better suited to the C language.

Let's change the question to this:

"I have an array of int, with 'len' elements, that I wish to populate
with 0s and 1s, depending on the value of an integer object with at
least 32 bytes. I need to do this sufficiently often that I want it to
be done by a function. How can I achieve this?"

Here is one such way:

#include <stddef.h>
#include <assert.h>

#define BITS_REQUIRED 32

void expand_bits(int *bit, size_t len, unsigned long n)
{
size_t thisbit = 0;
assert(len >= BITS_REQUIRED);
len = BITS_REQUIRED;
while(thisbit < len)
{
bit[thisbit++] = 1 & n;
n >>= 1;
}
}

/* test driver */
#include <stdio.h>

int main(void)
{
size_t i = 0;
int out[BITS_REQUIRED] = {0};
size_t len = sizeof out / sizeof out[0];
unsigned long in[] =
{
0, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,
0xF0000000, 0xFEFEFEFE, 0xFFFFFFFE, 0xFFFFFFFF
};
size_t test = 0;
while(test < sizeof in / sizeof in[0])
{
printf("Input: %lX\n", in[test]);
printf("Output (lsb first) : ");
expand_bits(out, len, in[test]);
i = 0;
while(i < len)
{
putchar('0' + out[i++]);
}
putchar('\n');
printf("Output (msb first) : ");
expand_bits(out, len, in[test]);
i = BITS_REQUIRED;
while(i-- > 0)
{
putchar('0' + out);
}
putchar('\n');
++test;
}
return 0;
}

When compiled and executed, this code produced the following output:

Input: 0
Output (lsb first) : 00000000000000000000000000000000
Output (msb first) : 00000000000000000000000000000000
Input: 1
Output (lsb first) : 10000000000000000000000000000000
Output (msb first) : 00000000000000000000000000000001
Input: 2
Output (lsb first) : 01000000000000000000000000000000
Output (msb first) : 00000000000000000000000000000010
Input: 3
Output (lsb first) : 11000000000000000000000000000000
Output (msb first) : 00000000000000000000000000000011
Input: 5
Output (lsb first) : 10100000000000000000000000000000
Output (msb first) : 00000000000000000000000000000101
Input: 8
Output (lsb first) : 00010000000000000000000000000000
Output (msb first) : 00000000000000000000000000001000
Input: D
Output (lsb first) : 10110000000000000000000000000000
Output (msb first) : 00000000000000000000000000001101
Input: 15
Output (lsb first) : 10101000000000000000000000000000
Output (msb first) : 00000000000000000000000000010101
Input: 22
Output (lsb first) : 01000100000000000000000000000000
Output (msb first) : 00000000000000000000000000100010
Input: 37
Output (lsb first) : 11101100000000000000000000000000
Output (msb first) : 00000000000000000000000000110111
Input: 59
Output (lsb first) : 10011010000000000000000000000000
Output (msb first) : 00000000000000000000000001011001
Input: F0000000
Output (lsb first) : 00000000000000000000000000001111
Output (msb first) : 11110000000000000000000000000000
Input: FEFEFEFE
Output (lsb first) : 01111111011111110111111101111111
Output (msb first) : 11111110111111101111111011111110
Input: FFFFFFFE
Output (lsb first) : 01111111111111111111111111111111
Output (msb first) : 11111111111111111111111111111110
Input: FFFFFFFF
Output (lsb first) : 11111111111111111111111111111111
Output (msb first) : 11111111111111111111111111111111
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top