发新帖我要提问
12
返回列表
打印

怎样判断一个变量里面是否有连续8个以上的1?

[复制链接]
楼主: forrest11
手机看帖
扫描二维码
随时随地手机跟帖
21
nethopper| | 2017-4-5 18:04 | 只看该作者 回帖奖励 |倒序浏览
本帖最后由 nethopper 于 2017-4-5 18:38 编辑

按LZ的方法做吧




使用特权

评论回复
22
zhuyemm| | 2017-4-5 18:28 | 只看该作者
好帖,赞一个,顶楼主

使用特权

评论回复
23
coody| | 2017-4-5 18:38 | 只看该作者
移位判断

使用特权

评论回复
24
zyj9490| | 2017-4-5 19:06 | 只看该作者
移位八次,每次跟0X00FF相与是否等于FF,就可以了。

使用特权

评论回复
25
oufuqiang| | 2017-4-5 19:40 | 只看该作者
#include <stdio.h>

int main()
{
unsigned int N;
for(N=0;N<65535;N++)
{
if((unsigned int)((N<<8)|(N>>8))==(unsigned int)(~N))
  {
  printf("the number %d have 8 of 1 continue,HEX is %x.\n",N,N);
  }
}
return(0);
}
这个代码找不出数据呢。
详见:
http://codepad.org/m1Iryhaw

使用特权

评论回复
26
oufuqiang| | 2017-4-5 19:51 | 只看该作者
这样才对。
http://codepad.org/Ex6glKym

#include <stdio.h>

int main()
{
unsigned int N,temp;
unsigned char i;
for(N=0;N<65535;N++)
{
temp=N;
for(i=0;i<8;i++)
{
if((temp&0x00ff)==0x00ff)
  {
  printf("the number %d have 8 of 1 continue,HEX is %x.\n",N,N);
  break;
  }
temp=temp>>1;
}
}
return(0);
}

the number 255 have 8 of 1 continue,HEX is ff.
the number 510 have 8 of 1 continue,HEX is 1fe.
the number 511 have 8 of 1 continue,HEX is 1ff.
the number 767 have 8 of 1 continue,HEX is 2ff.
the number 1020 have 8 of 1 continue,HEX is 3fc.
the number 1021 have 8 of 1 continue,HEX is 3fd.
the number 1022 have 8 of 1 continue,HEX is 3fe.
the number 1023 have 8 of 1 continue,HEX is 3ff.
the number 1279 have 8 of 1 continue,HEX is 4ff.
the number 1534 have 8 of 1 continue,HEX is 5fe.
the number 1535 have 8 of 1 continue,HEX is 5ff.
the number 1791 have 8 of 1 continue,HEX is 6ff.
the number 2040 have 8 of 1 continue,HEX is 7f8.
the number 2041 have 8 of 1 continue,HEX is 7f9.
the number 2042 have 8 of 1 continue,HEX is 7fa.
the number 2043 have 8 of 1 continue,HEX is 7fb.
the number 2044 have 8 of 1 continue,HEX is 7fc.
the number 2045 have 8 of 1 continue,HEX is 7fd.
the number 2046 have 8 of 1 continue,HEX is 7fe.
the number 2047 have 8 of 1 continue,HEX is 7ff.
the number 2303 have 8 of 1 continue,HEX is 8ff.
the number 2558 have 8 of 1 continue,HEX is 9fe.
the number 2559 have 8 of 1 continue,HEX is 9ff.
the number 2815 have 8 of 1 continue,HEX is aff.
the number 3068 have 8 of 1 continue,HEX is bfc.
the number 3069 have 8 of 1 continue,HEX is bfd.
the number 3070 have 8 of 1 continue,HEX is bfe.
the number 3071 have 8 of 1 continue,HEX is bff.
the number 3327 have 8 of 1 continue,HEX is cff.
the number 3582 have 8 of 1 continue,HEX is dfe.
the number 3583 have 8 of 1 continue,HEX is dff.
the number 3839 have 8 of 1 continue,HEX is eff.
the number 4080 have 8 of 1 continue,HEX is ff0.
the number 4081 have 8 of 1 continue,HEX is ff1.
the number 4082 have 8 of 1 continue,HEX is ff2.
the number 4083 have 8 of 1 continue,HEX is ff3.
the number 4084 have 8 of 1 continue,HEX is ff4.
the number 4085 have 8 of 1 continue,HEX is ff5.
the number 4086 have 8 of 1 continue,HEX is ff6.
the number 4087 have 8 of 1 continue,HEX is ff7.
the number 4088 have 8 of 1 continue,HEX is ff8.
the number 4089 have 8 of 1 continue,HEX is ff9.
the number 4090 have 8 of 1 continue,HEX is ffa.
the number 4091 have 8 of 1 continue,HEX is ffb.
the number 4092 have 8 of 1 continue,HEX is ffc.
the number 4093 have 8 of 1 continue,HEX is ffd.
the number 4094 have 8 of 1 continue,HEX is ffe.
the number 4095 have 8 of 1 continue,HEX is fff.
the number 4351 have 8 of 1 continue,HEX is 10ff.
the number 4606 have 8 of 1 continue,HEX is 11fe.
the number 4607 have 8 of 1 continue,HEX is 11ff.
the number 4863 have 8 of 1 continue,HEX is 12ff.
the number 5116 have 8 of 1 continue,HEX is 13fc.
the number 5117 have 8 of 1 continue,HEX is 13fd.
the number 5118 have 8 of 1 continue,HEX is 13fe.
the number 5119 have 8 of 1 continue,HEX is 13ff.
the number 5375 have 8 of 1 continue,HEX is 14ff.
the number 5630 have 8 of 1 continue,HEX is 15fe.
the number 5631 have 8 of 1 continue,HEX is 15ff.
the number 5887 have 8 of 1 continue,HEX is 16ff.
the number 6136 have 8 of 1 continue,HEX is 17f8.
the number 6137 have 8 of 1 continue,HEX is 17f9.
the number 6138 have 8 of 1 continue,HEX is 17fa.
the number 6139 have 8 of 1 continue,HEX is 17fb.
the number 6140 have 8 of 1 continue,HEX is 17fc.
the number 6141 have 8 of 1 continue,HEX is 17fd.
the number 6142 have 8 of 1 continue,HEX is 17fe.
the number 6143 have 8 of 1 continue,HEX is 17ff.
the number 6399 have 8 of 1 continue,HEX is 18ff.
the number 6654 have 8 of 1 continue,HEX is 19fe.
the number 6655 have 8 of 1 continue,HEX is 19ff.
the number 6911 have 8 of 1 continue,HEX is 1aff.
the number 7164 have 8 of 1 continue,HEX is 1bfc.
the number 7165 have 8 of 1 continue,HEX is 1bfd.
the number 7166 have 8 of 1 continue,HEX is 1bfe.
the number 7167 have 8 of 1 continue,HEX is 1bff.
the number 7423 have 8 of 1 continue,HEX is 1cff.
the number 7678 have 8 of 1 continue,HEX is 1dfe.
the number 7679 have 8 of 1 continue,HEX is 1dff.
the number 7935 have 8 of 1 continue,HEX is 1eff.
the number 8160 have 8 of 1 continue,HEX is 1fe0.
the number 8161 have 8 of 1 continue,HEX is 1fe1.
the number 8162 have 8 of 1 continue,HEX is 1fe2.
the number 8163 have 8 of 1 continue,HEX is 1fe3.
the number 8164 have 8 of 1 continue,HEX is 1fe4.
the number 8165 have 8 of 1 continue,HEX is 1fe5.
the number 8166 have 8 of 1 continue,HEX is 1fe6.
the number 8167 have 8 of 1 continue,HEX is 1fe7.
the number 8168 have 8 of 1 continue,HEX is 1fe8.
the number 8169 have 8 of 1 continue,HEX is 1fe9.
the number 8170 have 8 of 1 continue,HEX is 1fea.
the number 8171 have 8 of 1 continue,HEX is 1feb.
the number 8172 have 8 of 1 continue,HEX is 1fec.
the number 8173 have 8 of 1 continue,HEX is 1fed.
the number 8174 have 8 of 1 continue,HEX is 1fee.
the number 8175 have 8 of 1 continue,HEX is 1fef.
the number 8176 have 8 of 1 continue,HEX is 1ff0.
the number 8177 have 8 of 1 continue,HEX is 1ff1.
the number 8178 have 8 of 1 continue,HEX is 1ff2.
the number 8179 have 8 of 1 continue,HEX is 1ff3.
the number 8180 have 8 of 1 continue,HEX is 1ff4.
the number 8181 have 8 of 1 continue,HEX is 1ff5.
the number 8182 have 8 of 1 continue,HEX is 1ff6.
the number 8183 have 8 of 1 continue,HEX is 1ff7.
the number 8184 have 8 of 1 continue,HEX is 1ff8.
the number 8185 have 8 of 1 continue,HEX is 1ff9.
the number 8186 have 8 of 1 continue,HEX is 1ffa.
the number 8187 have 8 of 1 continue,HEX is 1ffb.
the number 8188 have 8 of 1 continue,HEX is 1ffc.
the number 8189 have 8 of 1 continue,HEX is 1ffd.
the number 8190 have 8 of 1 continue,HEX is 1ffe.
the number 8191 have 8 of 1 continue,HEX is 1fff.
the number 8447 have 8 of 1 continue,HEX is 20ff.
the number 8702 have 8 of 1 continue,HEX is 21fe.
the number 8703 have 8 of 1 continue,HEX is 21ff.
the number 8959 have 8 of 1 continue,HEX is 22ff.
the number 9212 have 8 of 1 continue,HEX is 23fc.
the number 9213 have 8 of 1 continue,HEX is 23fd.
the number 9214 have 8 of 1 continue,HEX is 23fe.
the number 9215 have 8 of 1 continue,HEX is 23ff.
the number 9471 have 8 of 1 continue,HEX is 24ff.
the number 9726 have 8 of 1 continue,HEX is 25fe.
the number 9727 have 8 of 1 continue,HEX is 25ff.
the number 9983 have 8 of 1 continue,HEX is 26ff.
the number 10232 have 8 of 1 continue,HEX is 27f8.
the number 10233 have 8 of 1 continue,HEX is 27f9.
the number 10234 have 8 of 1 continue,HEX is 27fa.
the number 10235 have 8 of 1 continue,HEX is 27fb.
the number 10236 have 8 of 1 continue,HEX is 27fc.
the number 10237 have 8 of 1 continue,HEX is 27fd.
the number 10238 have 8 of 1 continue,HEX is 27fe.
the number 10239 have 8 of 1 continue,HEX is 27ff.
the number 10495 have 8 of 1 continue,HEX is 28ff.
the number 10750 have 8 of 1 continue,HEX is 29fe.
the number 10751 have 8 of 1 continue,HEX is 29ff.
the number 11007 have 8 of 1 continue,HEX is 2aff.
the number 11260 have 8 of 1 continue,HEX is 2bfc.
the number 11261 have 8 of 1 continue,HEX is 2bfd.
the number 11262 have 8 of 1 continue,HEX is 2bfe.
the number 11263 have 8 of 1 continue,HEX is 2bff.
the number 11519 have 8 of 1 continue,HEX is 2cff.
the number 11774 have 8 of 1 continue,HEX is 2dfe.
the number 11775 have 8 of 1 continue,HEX is 2dff.
the number 12031 have 8 of 1 continue,HEX is 2eff.
the number 12272 have 8 of 1 continue,HEX is 2ff0.
the number 12273 have 8 of 1 continue,HEX is 2ff1.
the number 12274 have 8 of 1 continue,HEX is 2ff2.
the number 12275 have 8 of 1 continue,HEX is 2ff3.
the number 12276 have 8 of 1 continue,HEX is 2ff4.
the number 12277 have 8 of 1 continue,HEX is 2ff5.
the number 12278 have 8 of 1 continue,HEX is 2ff6.
the number 12279 have 8 of 1 continue,HEX is 2ff7.
the number 12280 have 8 of 1 continue,HEX is 2ff8.
the number 12281 have 8 of 1 continue,HEX is 2ff9.
the number 12282 have 8 of 1 continue,HEX is 2ffa.
the number 12283 have 8 of 1 continue,HEX is 2ffb.
the number 12284 have 8 of 1 continue,HEX is 2ffc.
the number 12285 have 8 of 1 continue,HEX is 2ffd.
the number 12286 have 8 of 1 continue,HEX is 2ffe.
the number 12287 have 8 of 1 continue,HEX is 2fff.
the number 12543 have 8 of 1 continue,HEX is 30ff.
the number 12798 have 8 of 1 continue,HEX is 31fe.
the number 12799 have 8 of 1 continue,HEX is 31ff.
the number 13055 have 8 of 1 continue,HEX is 32ff.
the number 13308 have 8 of 1 continue,HEX is 33fc.
the number 13309 have 8 of 1 continue,HEX is 33fd.
the number 13310 have 8 of 1 continue,HEX is 33fe.
the number 13311 have 8 of 1 continue,HEX is 33ff.
the number 13567 have 8 of 1 continue,HEX is 34ff.
the number 13822 have 8 of 1 continue,HEX is 35fe.
the number 13823 have 8 of 1 continue,HEX is 35ff.
the number 14079 have 8 of 1 continue,HEX is 36ff.
the number 14328 have 8 of 1 continue,HEX is 37f8.
the number 14329 have 8 of 1 continue,HEX is 37f9.
the number 14330 have 8 of 1 continue,HEX is 37fa.
the number 14331 have 8 of 1 continue,HEX is 37fb.
the number 14332 have 8 of 1 continue,HEX is 37fc.
the number 14333 have 8 of 1 continue,HEX is 37fd.
the number 14334 have 8 of 1 continue,HEX is 37fe.
the number 14335 have 8 of 1 continue,HEX is 37ff.
the number 14591 have 8 of 1 continue,HEX is 38ff.
the number 14846 have 8 of 1 continue,HEX is 39fe.
the number 14847 have 8 of 1 continue,HEX is 39ff.
the number 15103 have 8 of 1 continue,HEX is 3aff.
the number 15356 have 8 of 1 continue,HEX is 3bfc.
the number 15357 have 8 of 1 continue,HEX is 3bfd.
the number 15358 have 8 of 1 continue,HEX is 3bfe.
the number 15359 have 8 of 1 continue,HEX is 3bff.
the number 15615 have 8 of 1 continue,HEX is 3cff.
the number 15870 have 8 of 1 continue,HEX is 3dfe.
the number 15871 have 8 of 1 continue,HEX is 3dff.
the number 16127 have 8 of 1 continue,HEX is 3eff.
the number 16320 have 8 of 1 continue,HEX is 3fc0.
the number 16321 have 8 of 1 continue,HEX is 3fc1.
the number 16322 have 8 of 1 continue,HEX is 3fc2.
the number 16323 have 8 of 1 continue,HEX is 3fc3.
the number 16324 have 8 of 1 continue,HEX is 3fc4.
the number 16325 have 8 of 1 continue,HEX is 3fc5.
the number 16326 have 8 of 1 continue,HEX is 3fc6.
the number 16327 have 8 of 1 continue,HEX is 3fc7.
the number 16328 have 8 of 1 continue,HEX is 3fc8.
the number 16329 have 8 of 1 continue,HEX is 3fc9.
the number 16330 have 8 of 1 continue,HEX is 3fca.
the number 16331 have 8 of 1 continue,HEX is 3fcb.
the number 16332 have 8 of 1 continue,HEX is 3fcc.
the number 16333 have 8 of 1 continue,HEX is 3fcd.
the number 16334 have 8 of 1 continue,HEX is 3fce.
the number 16335 have 8 of 1 continue,HEX is 3fcf.
the number 16336 have 8 of 1 continue,HEX is 3fd0.
the number 16337 have 8 of 1 continue,HEX is 3fd1.
the number 16338 have 8 of 1 continue,HEX is 3fd2.
the number 16339 have 8 of 1 continue,HEX is 3fd3.
the number 16340 have 8 of 1 continue,HEX is 3fd4.
the number 16341 have 8 of 1 continue,HEX is 3fd5.
the number 16342 have 8 of 1 continue,HEX is 3fd6.
the number 16343 have 8 of 1 continue,HEX is 3fd7.
the number 16344 have 8 of 1 continue,HEX is 3fd8.
the number 16345 have 8 of 1 continue,HEX is 3fd9.

使用特权

评论回复
27
menfiss| | 2017-4-5 21:28 | 只看该作者
模式匹配

使用特权

评论回复
28
nethopper| | 2017-4-5 22:06 | 只看该作者

LZ应该是想找个简单的判别准则,不想用常规的循环判别,所以开始我用if((WORD)((N<<8)|(N>>8))==(WORD)(~N))作一次性判别无需循环, 后来细想了一下,感觉不妥,因为只要是“反对称”的16位排列都满足条件,所以放弃了,还是老老实实循环判别吧。

其实就是255*(2^N), N =0,1,...,7,8

使用特权

评论回复
29
oufuqiang| | 2017-4-5 22:37 | 只看该作者
nethopper 发表于 2017-4-5 22:06
LZ应该是想找个简单的判别准则,不想用常规的循环判别,所以开始我用if((WORD)((N8))==(WORD)(~N))作一次 ...

其实循环也只是循环8次,外面那个65535是验证测试用的。代码效率应该不会差到哪里去。

使用特权

评论回复
30
zxq6| | 2017-4-5 22:54 | 只看该作者
如果说你还有多余的定时器和spi(uart)口,可以考虑将需要判断的数字,通过spi发送出去,mosi接到定时器输入捕获上,判断高电平连续持续时间,这样可以判断是否有连续的8个1了。

使用特权

评论回复
31
forrest11|  楼主 | 2017-4-6 00:23 | 只看该作者
本帖最后由 forrest11 于 2017-4-6 00:26 编辑

由于如果在中断处理bit数据,带来的问题是所有的收数据处理都必须在中断中处理,这样中断函数太大了,不合理。
所以还是回到初始,在主程序中处理input data。这样中断函数必须记录接收到的数据序列和bit个数。
回到主程序中,问题又来了:bit个数不一定是16位,而是一个变化数字。要考虑的事情有:
1.    等待与下一批数据拼接.
2.   检查bit个数是否大于7,如果没有,则跳到5;(其实处理也可以,比如记录现有的1bit,但太复杂了)
3.   查数列中有没有连续8个1,检测的办法总是有的;
4.   如果检测到了,则到7
5.   找到最后一个0bit,保留右边的全部1bit,左边的数据全部丢弃,更新有效bit个数
6.   回到1;
7.   到下一个状态

你们有更好的办法吗?

使用特权

评论回复
32
forrest11|  楼主 | 2017-4-6 10:33 | 只看该作者
caosix 发表于 2017-4-6 09:45
参照 TPC/IP 的 “子网掩码” 概念。

先对 输入的16Bit数据 做掩码 啊。

做掩码?不行吧,你写FFxx。。。。。xxFF 在case语句里面应该综合不了的。
如果一个一个做掩码判断,这样的方法我无话可说。用硬件这么搭电路真是罪过。

使用特权

评论回复
33
cos12a| | 2017-4-6 22:09 | 只看该作者
if(i &=(i =<<1));
循环比较8次还>0 就有>8个连续1.
最大判断次数8次.

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则