位操纵问题 [英] bit manipulation question

查看:46
本文介绍了位操纵问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个长x;

我想写一个函数


long f(long x,int k)


这样它可以提取x的每个第k位,连接它们并返回它们并返回它。任何人都可以帮我写这个

函数吗?


例子

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


这里的任何一位大师都可以帮助我吗?


谢谢,

- -Elijah

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

推荐答案

ge ****** @ hotmail.com (Elijah Bailey)写道:
ge******@hotmail.com (Elijah Bailey) writes:
我想写一个函数

long f(long x,int k)

这样它可以提取x的每个第k位,连接它们并返回它。任何人都可以帮我写这个
函数吗?

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 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




我不明白你的k = 3例子。不应该(x)= 101和其他人一致吗?


/ *我认为这会有所作为,但我没有没有测试它甚至

编译它。事实上,我甚至没有在纸上试过,

所以你最好仔细考虑一下,然后再假设

。 * /

unsigned long f(unsigned long x,int k)

{

unsigned long out = 0; / *输出到目前为止。 * /

unsigned long left = -1; / *从x中提取的位掩码。 * /

unsigned long msb =〜(left<< 1>> 1); / *无符号长整数位。 * /


do {

/ *将x的MSB提取到LSB中。 * /

out<< = 1;

if(x& msb)

out | = 1;


/ * Advance。 * /

x<< = k;

left<< = k;

} while(left!= 0);


退出;

}

-

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] \

);}返回0;}



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;

/* Advance. */
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;}


Elijah Bailey写道:
Elijah Bailey wrote:
我有一个很长的x;
我想写一个函数

long f(long x,int k)

这样它可以提取每个第k位x,连接它们并返回它。任何人都可以帮我写这个
函数吗?

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 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




虽然这对我来说就像是作业一样,但我不能'' t抵抗

尝试一些东西。

我不会像你在你的例子中那样试着调用它,因为我没有
认为任何C编译器都会编译它。


此代码未经测试,不会处理任何特殊情况。至少在明显的一个上有

,但你可以自己处理。


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);


返回温度;

}


马克


Capstar写道:
Capstar wrote:
Elijah Bailey写道:
Elijah Bailey wrote:
我有一个很长的x;
我想写一个函数长f(long x,int k)

这样它就可以提取x的每个第k位,连接它们并返回它。任何人都可以帮我写这个
函数吗?
例子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 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



虽然这对我来说就像作业一样,但我无法抗拒尝试一些东西。我不会像你在你的例子中那样尝试调用它,因为我不认为任何C编译器都会编译它。

这段代码是未经测试的,不会注意任何特殊情况。至少在显而易见的情况下,你可以自己处理。

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);

返回温度;
}
标记


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




嗯,之后再次阅读你的帖子,我似乎犯了一点

错误。在我之前的帖子中,你总是从第0位开始。但是当你说每个第k位时你需要
,你显然想从第k-1位开始。


我也对它进行了测试,因此我将发布完整的测试程序:

#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)* 8)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'';


返回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);


返回临时;

}


int main(void)

{

long x = 0xaa; / * 10101010 * /

int k;

char * xstring;


xstring = strdup(bitprint(x,8) );


for(k = 1; k <= 4; ++ k)

printf(" x =%s\tk = %d \ tf(x,k)=%s \ n",xstring,k,bitprint(f(x,

k),8));


免费(xstring);


返回0;

}


马克


PS。这个标准是不是很严格?如果我用gcc -W -Wall编译这个

-ansi -pedantic我得到:警告:隐式声明函数`strdup''。



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) * 8) 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, 8));

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

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''.


这篇关于位操纵问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆