有趣的任务 - 任何优化的方法来找出一个单词中是否存在连续性! [英] Interesting Quest - Any optimized way to find if consective one's exist in a word!

查看:60
本文介绍了有趣的任务 - 任何优化的方法来找出一个单词中是否存在连续性!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


任何人都可以知道如何在C中使用按位操作符来确定存在的连续性$

a word / dword语言。


显然这可以通过逐个移位和循环来完成;但是

必须有一些更快的方法。


我想要的是定义一些面具和宏并找出是否有

是单词/双字上的连续单词。例如0000b没有任何后果.......
... 00011000b有两个连续的,0001101 ...没有

连续的000001000有一个连续的!所以请求

来确定两者之间是否存在任何错误。


最好的问候,

Shafique

解决方案

shafique写道:

任何人都有想法如何找到它存在的连续性''使用C语言中的按位运算符的单词/双字中的



换一个和。


我想要的是定义一些面具和宏,并找出是否有连续性关于单词/双关语的。例如0000b没有连带的...... 00011000b有两个连续的,0001101 ...没有连续的000001000有一个连续的!因此,请求
是要找出两个之间是否存在任何错误。




本段中的错误很多(如果第一段是

右)。


Daniel Vallstrom


" shafique" <是ne *********** @ lycos.co.uk>在消息中写道

news:11 ********************* @ l41g2000cwc.googlegro ups.com ...

任何人都可以知道如何找到它在C / C语言中使用按位操作符的单词/双字中存在的连续性。


我从你的(剪掉的)例子中推断出你真正想要确定的是什么

是值是否为形式(2 ^ n - 1)* 2 ^ m,其中n和m

是n> 1的整数0和m> = 0(''^''表示取幂,而不是C

XOR运算符)。目前尚不清楚你是否需要知道n和/或m是什么。


请澄清。

显然这可以通过转移和一个接一个地循环;但是必须有一些更快的方法。




可能。


Alex


>任何人都有想法如何找到它在

使用C语言中的按位操作符的单词/双字中存在连续的'


我想要的是定义一些掩码和宏,并找出
是否是单词/双字上的连续词。例如0000b没有连带的...... 00011000b有两个连续的,0001101 ...没有连续的000001000有一个连续的!所以请求
是要知道两个之间是否存在任何0错误。




我怀疑如果你发现只有位数算法使用位

移位和掩码,然后弄清楚它是如何工作的,为什么它可以工作,你可能会想出一些符合你规格的东西。

就个人而言,我能做的最好的是:


#define BAD 0

#define ALL_ZERO 1

#define MIDDLE 2

#define LEFT 4

#define RIGHT 8

#define BOTH 12 //位掩码覆盖LEFT和RIGHT


int onlyHasConsecutiveBits(int test){//如果不好则返回0,如果好则返回1

int L;

int possibilities [] = {

ALL_ZERO,// 0000

RIGHT,// 0001

MIDDLE,// 0010

RIGHT,/ / 0011

MIDDLE,// 0100

BAD,// 0101

MIDDLE,// 0110

RIGHT ,// 0111

LEFT,// 1000

BAD,// 1001
BAD,// 1010

BAD,// 1011

LEFT,// 1100

BAD,// 1101

LEFT,// 1110

BOTH // 1111

};


int previousfound = ALL_ZERO;

int wasGap = 0;

for(L = 0; L< 8; L ++){

int result = possibilities [test& 0xf];

test>> = 4;


开关(结果){

case BAD:

返回0;

案例权利:

案例两种情况:

if(

(previousfound) != ALL_ZERO&&!(previousFound& LEFT)))||

wasGap

){

返回0;

}

休息;

案例左边:

案例MIDDLE:

if(previousFound) != ALL_ZERO){

返回0;

}

休息;

案例ALL_ZERO:

if(previousFound!= ALL_ZERO){

wasGap = 1;

}

休息;

}

如果(结果!= ALL_ZERO){

previousFound =结果;

}

}


if(previousFound == ALL_ZERO){

返回0;

}

返回1; <我刚刚写在那里的
}


,所以它可能会有错误和错别字

等等。但这样的方法至少会比逐位检查更好。


-Chris


Hello,

Can anybody have idea as how to find it there exist consective one''s in
a word/dword using bitwise operatiors in C language.

Obviously this can be done by shifting and looping one by one; but
there must be some faster way of doing it.

The thing I want is to define some masks and macros and find if there
are consective ones on a word/dword. e.g. 0000b has no consective
ones.... 00011000b has two consective ones, 0001101... has no
consective ones 000001000 has one consective one! So the requiement is
to find out if there exists any 0 sandwitched between two ones.

Best Regards,
Shafique

解决方案

shafique wrote:

Can anybody have idea as how to find it there exist consective one''s in a word/dword using bitwise operatiors in C language.
Shift one and and.

The thing I want is to define some masks and macros and find if there
are consective ones on a word/dword. e.g. 0000b has no consective
ones.... 00011000b has two consective ones, 0001101... has no
consective ones 000001000 has one consective one! So the requiement is to find out if there exists any 0 sandwitched between two ones.



Errors are abundant in this paragraph (if the first paragraph is
right).

Daniel Vallstrom


"shafique" <ne***********@lycos.co.uk> wrote in message
news:11*********************@l41g2000cwc.googlegro ups.com...

Can anybody have idea as how to find it there exist consective one''s in
a word/dword using bitwise operatiors in C language.
I infer from your (snipped) examples that what you really want to determine
is whether or not the value is of the form (2^n - 1) * 2^m, where n and m
are integers with n > 0 and m >= 0 (''^'' denotes exponentiation, not the C
XOR operator). It''s not clear if you need to know what n and/or m are.

Please clarify.
Obviously this can be done by shifting and looping one by one; but
there must be some faster way of doing it.



Probably.

Alex


> Can anybody have idea as how to find it there exist consective one''s
in

a word/dword using bitwise operatiors in C language.

Obviously this can be done by shifting and looping one by one; but
there must be some faster way of doing it.

The thing I want is to define some masks and macros and find if there
are consective ones on a word/dword. e.g. 0000b has no consective
ones.... 00011000b has two consective ones, 0001101... has no
consective ones 000001000 has one consective one! So the requiement is to find out if there exists any 0 sandwitched between two ones.



I suspect that if you find the bit-count algorithm that only uses bit
shifts and masks and then figure out how and why it works, you may be
able to come up with something that fits your specification.
Personally, the best I can do is:

#define BAD 0
#define ALL_ZERO 1
#define MIDDLE 2
#define LEFT 4
#define RIGHT 8
#define BOTH 12 //Bit mask covering LEFT and RIGHT

int onlyHasConsecutiveBits(int test) { //returns 0 if bad, 1 if good
int L;
int possibilities[] = {
ALL_ZERO, //0000
RIGHT, //0001
MIDDLE, //0010
RIGHT, //0011
MIDDLE, //0100
BAD, //0101
MIDDLE, //0110
RIGHT, //0111
LEFT, //1000
BAD, //1001
BAD, //1010
BAD, //1011
LEFT, //1100
BAD, //1101
LEFT, //1110
BOTH //1111
};

int previouslyFound = ALL_ZERO;
int wasGap = 0;
for (L = 0; L < 8; L++) {
int result = possibilities[test & 0xf];
test >>= 4;

switch (result) {
case BAD:
return 0;
case RIGHT:
case BOTH:
if (
(previouslyFound != ALL_ZERO && !(previouslyFound & LEFT))) ||
wasGap
) {
return 0;
}
break;
case LEFT:
case MIDDLE:
if (previouslyFound != ALL_ZERO) {
return 0;
}
break;
case ALL_ZERO:
if (previouslyFound != ALL_ZERO) {
wasGap = 1;
}
break;
}
if (result != ALL_ZERO) {
previouslyFound = result;
}
}

if (previouslyFound == ALL_ZERO) {
return 0;
}
return 1;
}

which I just wrote in there, so it probably will have bugs and typos
and such. But such a method will at least be better than a bit-by-bit
check.

-Chris


这篇关于有趣的任务 - 任何优化的方法来找出一个单词中是否存在连续性!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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