Velocity Reviews > bit manipulation question

# bit manipulation question

Elijah Bailey
Guest
Posts: n/a

 12-03-2003
I have a long x;
I want to write a function

long f(long x, int k)

such that it extracts every k-th bit of x, concatenates
them and returns it. Anyone can help me in writing this
function?

examples
x = 10101010 k = 1 f(x) = 10101010
x = 10101010 k = 2 f(x) = 1111
x = 10101010 k = 3 f(x) = 010
x = 10101010 k = 4 f(x) = 11

Any bit gurus here who can help me?

Thanks,
--Elijah

Ben Pfaff
Guest
Posts: n/a

 12-03-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Elijah Bailey) writes:

> I want to write a function
>
> long f(long x, int k)
>
> such that it extracts every k-th bit of x, concatenates
> them and returns it. Anyone can help me in writing this
> function?
>
> examples
> x = 10101010 k = 1 f(x) = 10101010
> x = 10101010 k = 2 f(x) = 1111
> x = 10101010 k = 3 f(x) = 010
> x = 10101010 k = 4 f(x) = 11

I don't understand your k = 3 example. Shouldn't f(x) = 101 for
consistency with the others?

/* I think this'll do the trick, but I haven't tested it or even
compiled it. In fact, I haven't even tried it out on paper,
so you'd better think it through carefully before assuming
anything. */
unsigned long f(unsigned long x, int k)
{
unsigned long out = 0; /* Output so far. */
unsigned long left = -1; /* Mask of bits left to extract from x. */
unsigned long msb = ~(left << 1 >> 1); /* Top bit in a unsigned long. */

do {
/* Extract MSB of x into LSB of out. */
out <<= 1;
if (x & msb)
out |= 1;

x <<= k;
left <<= k;
} while (left != 0);

return out;
}
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}

Capstar
Guest
Posts: n/a

 12-03-2003
Elijah Bailey wrote:
> I have a long x;
> I want to write a function
>
> long f(long x, int k)
>
> such that it extracts every k-th bit of x, concatenates
> them and returns it. Anyone can help me in writing this
> function?
>
> examples
> x = 10101010 k = 1 f(x) = 10101010
> x = 10101010 k = 2 f(x) = 1111
> x = 10101010 k = 3 f(x) = 010
> x = 10101010 k = 4 f(x) = 11
>
> Any bit gurus here who can help me?
>
> Thanks,
> --Elijah

Although this seems to me like a homework asignment, I couldn't resist
to try something.
I wouldn't try calling it as you do in your example, because I don't
think any C compiler would compile it.

This code is untested and does not take care any special cases. There is
at least on obvious one, but you can take care of that yourself.

long f(long x, int k)
{
long temp = 0;
long i, j;

for(i = 0, j = 0; i < sizeof(x) * 8; i += k, ++j)
temp |= (x & (1 << i)) >> (i - j);

return temp;
}

Mark

Capstar
Guest
Posts: n/a

 12-03-2003
Capstar wrote:
> Elijah Bailey wrote:
>
>> I have a long x;
>> I want to write a function
>> long f(long x, int k)
>>
>> such that it extracts every k-th bit of x, concatenates
>> them and returns it. Anyone can help me in writing this
>> function?
>> examples
>> x = 10101010 k = 1 f(x) = 10101010
>> x = 10101010 k = 2 f(x) = 1111
>> x = 10101010 k = 3 f(x) = 010 x = 10101010 k = 4 f(x) = 11
>>
>> Any bit gurus here who can help me?
>>
>> Thanks,
>> --Elijah

>
>
> Although this seems to me like a homework asignment, I couldn't resist
> to try something.
> I wouldn't try calling it as you do in your example, because I don't
> think any C compiler would compile it.
>
> This code is untested and does not take care any special cases. There is
> at least on obvious one, but you can take care of that yourself.
>
> long f(long x, int k)
> {
> long temp = 0;
> long i, j;
>
> for(i = 0, j = 0; i < sizeof(x) * 8; i += k, ++j)
> temp |= (x & (1 << i)) >> (i - j);
>
> return temp;
> }
>
> Mark
>

Hmmm, after reading your post again, I seem to have made a little
mistake. In my previous post you always start at bit 0. But when you say
take every k-th bit, you obviously want to start at bit k - 1.

I also tested it so I'll post my complete test program:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *bitprint(long x, unsigned long max)
{
unsigned long i, j;
static char bitstring[sizeof(x) * 8 + 1];

if(max > sizeof(x) * max = sizeof(x) * 8;

for(i = max - 1, j = 0; j < max; --i, ++j)
{
if(x & (1 << i)) bitstring[j] = '1';
else bitstring[j] = '0';
}
bitstring[max] = '\0';

return bitstring;
}

long f(long x, int k)
{
long temp = 0;
unsigned long i, j;

for(i = k - 1, j = 0; i < sizeof(x) * 8; i += k, ++j)
temp |= (x & (1 << i)) >> (i - j);

return temp;
}

int main(void)
{
long x = 0xaa; /*10101010*/
int k;
char *xstring;

xstring = strdup(bitprint(x, );

for(k = 1; k <= 4; ++k)
printf("x = %s\tk = %d\t f(x, k) = %s\n", xstring, k, bitprint(f(x,
k), );

free(xstring);

return 0;
}

Mark

PS. Isn't strdup in the standard? If I compile this with gcc -W -Wall
-ansi -pedantic I get: warning: implicit declaration of function `strdup'.

nrk
Guest
Posts: n/a

 12-03-2003
<snip>

>
> PS. Isn't strdup in the standard? If I compile this with gcc -W -Wall
> -ansi -pedantic I get: warning: implicit declaration of function `strdup'.

No, strdup is *not* part of the standard.

<OT>
According to my manpage, conforms to SYS V R3 and BSD 4.3
</OT>

-nrk.

Mark Gordon
Guest
Posts: n/a

 12-03-2003
On Wed, 03 Dec 2003 10:10:04 +0100
Capstar <(E-Mail Removed)> wrote:

> Capstar wrote:
> > Elijah Bailey wrote:
> >
> >> I have a long x;
> >> I want to write a function
> >> long f(long x, int k)
> >>
> >> such that it extracts every k-th bit of x, concatenates
> >> them and returns it. Anyone can help me in writing this
> >> function?
> >> examples
> >> x = 10101010 k = 1 f(x) = 10101010
> >> x = 10101010 k = 2 f(x) = 1111
> >> x = 10101010 k = 3 f(x) = 010 x = 10101010 k = 4 f(x) = 11
> >>
> >> Any bit gurus here who can help me?
> >>
> >> Thanks,
> >> --Elijah

> >
> >
> > Although this seems to me like a homework asignment, I couldn't
> > resist to try something.
> > I wouldn't try calling it as you do in your example, because I don't
> > think any C compiler would compile it.
> >
> > This code is untested and does not take care any special cases.
> > There is at least on obvious one, but you can take care of that
> > yourself.
> >
> > long f(long x, int k)
> > {
> > long temp = 0;
> > long i, j;
> >
> > for(i = 0, j = 0; i < sizeof(x) * 8; i += k, ++j)

Tut, tut. Assuming CHAR_BIT==8

> > temp |= (x & (1 << i)) >> (i - j);
> >
> > return temp;
> > }
> >
> > Mark
> >

>
> Hmmm, after reading your post again, I seem to have made a little
> mistake. In my previous post you always start at bit 0. But when you
> say take every k-th bit, you obviously want to start at bit k - 1.
>
> I also tested it so I'll post my complete test program:
> #include <stdio.h>
> #include <string.h>
> #include <stdlib.h>
>
> char *bitprint(long x, unsigned long max)

I would prefer to have the function take a parameter to the buffer
to be used rather than returning a pointer to a static buffer.

> {
> unsigned long i, j;
> static char bitstring[sizeof(x) * 8 + 1];

Tut, tut. Still assuming CHAR_BIT==8 throughout this new version.

> if(max > sizeof(x) * max = sizeof(x) * 8;
>
> for(i = max - 1, j = 0; j < max; --i, ++j)
> {
> if(x & (1 << i)) bitstring[j] = '1';
> else bitstring[j] = '0';
> }
> bitstring[max] = '\0';
>
> return bitstring;
> }
>
> long f(long x, int k)
> {
> long temp = 0;
> unsigned long i, j;
>
> for(i = k - 1, j = 0; i < sizeof(x) * 8; i += k, ++j)
> temp |= (x & (1 << i)) >> (i - j);
>
> return temp;
> }
>
> int main(void)
> {
> long x = 0xaa; /*10101010*/
> int k;
> char *xstring;
>
> xstring = strdup(bitprint(x, );
>
> for(k = 1; k <= 4; ++k)
> printf("x = %s\tk = %d\t f(x, k) = %s\n", xstring, k,
> bitprint(f(x,
> k), );
>
> free(xstring);
>
> return 0;
> }
>
> Mark
>
> PS. Isn't strdup in the standard? If I compile this with gcc -W -Wall
> -ansi -pedantic I get: warning: implicit declaration of function
> `strdup'.

No, it's not part of the standard and so when you use -ansi -pedantic it
is not defined by string.h
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.

Grumble
Guest
Posts: n/a

 12-03-2003
Ben Pfaff wrote:
> Elijah Bailey writes:
>
>>I want to write a function
>>
>>long f(long x, int k)
>>
>>such that it extracts every k-th bit of x, concatenates
>>them and returns it. Anyone can help me in writing this
>>function?
>>
>>examples
>> x = 10101010 k = 1 f(x) = 10101010
>> x = 10101010 k = 2 f(x) = 1111
>> x = 10101010 k = 3 f(x) = 010
>> x = 10101010 k = 4 f(x) = 11

>
>
> I don't understand your k = 3 example. Shouldn't f(x) = 101 for
> consistency with the others?

Let x_i denote the i_th bit in x counting from right to left.

In particular, x_0 denotes the right-most bit in x.

Then f(x)_i = x_{k-1+i*k}

k=1 => f(x)_i = x_i
k=2 => f(x)_i = x_{2*i+1}
k=3 => f(x)_i = x_{3*i+2}

Thus, from right to left, x_2 | x_5 | x_8

nrk
Guest
Posts: n/a

 12-03-2003
Elijah Bailey wrote:

> I have a long x;
> I want to write a function
>
> long f(long x, int k)
>
> such that it extracts every k-th bit of x, concatenates
> them and returns it. Anyone can help me in writing this
> function?
>
> examples
> x = 10101010 k = 1 f(x) = 10101010
> x = 10101010 k = 2 f(x) = 1111
> x = 10101010 k = 3 f(x) = 010
> x = 10101010 k = 4 f(x) = 11
>
> Any bit gurus here who can help me?
>
> Thanks,
> --Elijah

Probably a bit of beating around the bush going on in the code below, but
hey, you can verify your function as well

-nrk.

#include <stdio.h>
#include <limits.h>
#include <string.h>

unsigned long bitextract(unsigned long input, int k) {
unsigned long ret = 0;
size_t i;
int j;

if ( k <= 1 ) return input;

for ( i = 0, j = 0; i < sizeof input * CHAR_BIT; i += k, ++j ) {
input >>= (k-1);
ret |= (input & 1) << j;
input >>= 1;
}

return ret;
}

char *printBinary(char *buf, unsigned long num) {
static char *hex2bin[] =
{
"0000", "0001", "0010", "0011",
"0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011",
"1100", "1101", "1110", "1111"
};

size_t i;

for ( i = 4; i <= (sizeof num * CHAR_BIT); i += 4 ) {
int shift = (sizeof num * CHAR_BIT) - i;
memcpy(buf, hex2bin[((num & (0xFUL << shift)) >> shift)], 4);
buf += 4;
}
*buf = 0;

return buf - (i - 4);
}

int verifyResult(int input, int k, int result) {
char input_str[sizeof input * CHAR_BIT + 1];
char result_str[sizeof input * CHAR_BIT + 1];
char extract_str[sizeof input * CHAR_BIT + 1];
int i, j;

printBinary(input_str, input);
printBinary(result_str, result);

for ( i = sizeof input_str - 1 - k, j = sizeof extract_str - 2;
i >= 0;
i -= k, --j )
{
extract_str[j] = input_str[i];
}

while ( j >= 0 ) extract_str[j--] = '0';

extract_str[sizeof extract_str - 1] = 0;

return strcmp(result_str, extract_str);
}

int main(void) {
unsigned long input = 0x12345678UL;
unsigned long result;
size_t i;

for ( i = 1; i <= sizeof input * CHAR_BIT; ++i ) {
result = bitextract(input, i);
if ( verifyResult(input, i, result) ) {
printf("Failed for bit %d\n", i);
break;
}
}

if ( i >= sizeof input * CHAR_BIT )
printf("Success!\n");

return 0;
}

Capstar
Guest
Posts: n/a

 12-03-2003
Mark Gordon wrote:
> On Wed, 03 Dec 2003 10:10:04 +0100
> Capstar <(E-Mail Removed)> wrote:
>
>
>>Capstar wrote:
>>
>>>Elijah Bailey wrote:
>>>
>>>
>>>>I have a long x;
>>>>I want to write a function
>>>>long f(long x, int k)
>>>>
>>>>such that it extracts every k-th bit of x, concatenates
>>>>them and returns it. Anyone can help me in writing this
>>>>function?
>>>>examples
>>>> x = 10101010 k = 1 f(x) = 10101010
>>>> x = 10101010 k = 2 f(x) = 1111
>>>> x = 10101010 k = 3 f(x) = 010 x = 10101010 k = 4 f(x) = 11
>>>>
>>>>Any bit gurus here who can help me?
>>>>
>>>>Thanks,
>>>>--Elijah
>>>
>>>
>>>Although this seems to me like a homework asignment, I couldn't
>>>resist to try something.
>>>I wouldn't try calling it as you do in your example, because I don't
>>>think any C compiler would compile it.
>>>
>>>This code is untested and does not take care any special cases.
>>>There is at least on obvious one, but you can take care of that
>>>yourself.
>>>
>>>long f(long x, int k)
>>>{
>>> long temp = 0;
>>> long i, j;
>>>
>>> for(i = 0, j = 0; i < sizeof(x) * 8; i += k, ++j)

>
>
> Tut, tut. Assuming CHAR_BIT==8

I am not a c.l.c. guru and so I'm not an allmighty know it all. And I
thought sizeof returned in 8-bit bytes. I'll keep CHAR_BIT in mind next
time.

>
>
>>> temp |= (x & (1 << i)) >> (i - j);
>>>
>>> return temp;
>>>}
>>>
>>>Mark
>>>

>>
>>Hmmm, after reading your post again, I seem to have made a little
>>mistake. In my previous post you always start at bit 0. But when you
>>say take every k-th bit, you obviously want to start at bit k - 1.
>>
>>I also tested it so I'll post my complete test program:
>>#include <stdio.h>
>>#include <string.h>
>>#include <stdlib.h>
>>
>>char *bitprint(long x, unsigned long max)

>
>
> I would prefer to have the function take a parameter to the buffer
> to be used rather than returning a pointer to a static buffer.

I don't realy care wat you want. I just needed a function, which would
return a character array so I could display the value of f(x,k) to
verify the results easily.

>
>
>>{
>> unsigned long i, j;
>> static char bitstring[sizeof(x) * 8 + 1];

>
>
> Tut, tut. Still assuming CHAR_BIT==8 throughout this new version.

I didn't became an allmighty know it all c.l.c. guru in those 32
minutes, So I still thought sizeof returned in 8-bit bytes.

>
>
>> if(max > sizeof(x) * max = sizeof(x) * 8;
>>
>> for(i = max - 1, j = 0; j < max; --i, ++j)
>> {
>> if(x & (1 << i)) bitstring[j] = '1';
>> else bitstring[j] = '0';
>> }
>> bitstring[max] = '\0';
>>
>> return bitstring;
>>}
>>
>>long f(long x, int k)
>>{
>> long temp = 0;
>> unsigned long i, j;
>>
>> for(i = k - 1, j = 0; i < sizeof(x) * 8; i += k, ++j)
>> temp |= (x & (1 << i)) >> (i - j);
>>
>> return temp;
>>}
>>
>>int main(void)
>>{
>> long x = 0xaa; /*10101010*/
>> int k;
>> char *xstring;
>>
>> xstring = strdup(bitprint(x, );
>>
>> for(k = 1; k <= 4; ++k)
>> printf("x = %s\tk = %d\t f(x, k) = %s\n", xstring, k,
>> bitprint(f(x,
>>k), );
>>
>> free(xstring);
>>
>> return 0;
>>}
>>
>>Mark
>>
>>PS. Isn't strdup in the standard? If I compile this with gcc -W -Wall
>>-ansi -pedantic I get: warning: implicit declaration of function
>>`strdup'.

>
>
> No, it's not part of the standard and so when you use -ansi -pedantic it
> is not defined by string.h

OK, thanks.

Mark Gordon
Guest
Posts: n/a

 12-03-2003
On Wed, 03 Dec 2003 17:24:09 +0100
Capstar <(E-Mail Removed)> wrote:

> Mark Gordon wrote:

<snip>

> > Tut, tut. Assuming CHAR_BIT==8

>
> I am not a c.l.c. guru and so I'm not an allmighty know it all.

Nor am I. Perhaps I should not have said "Tut, tut" since it was not my
intent to cause offence.

> And I
> thought sizeof returned in 8-bit bytes. I'll keep CHAR_BIT in mind
> next time.

Well, I've been caught out a few times here. One of the good things
about this group is that people do tend to pick up on the assumptions we
(and I do deliberately include myself) make.

<snip>

> > I would prefer to have the function take a parameter to the buffer
> > to be used rather than returning a pointer to a static buffer.

>
> I don't realy care wat you want. I just needed a function, which would
> return a character array so I could display the value of f(x,k) to
> verify the results easily.

It was merely intended as a helpful stylistic comment, not an
instruction.

<snip>

> > No, it's not part of the standard and so when you use -ansi
> > -pedantic it is not defined by string.h

>
> OK, thanks.

You're welcome.
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.