Chinaunix

标题: 笔试题:如何求一个char中有几个1? [打印本页]

作者: lucky_han    时间: 2005-03-16 19:33
标题: 笔试题:如何求一个char中有几个1?
晕,没有做出来。要求一个在速度上优化,一个在内存占用上优化。
作者: aero    时间: 2005-03-16 21:25
标题: 笔试题:如何求一个char中有几个1?
用1,左移8次和这个char相与,判断,然后累计。这样可以吧。
作者: assiss    时间: 2005-03-16 22:00
标题: 笔试题:如何求一个char中有几个1?
[quote]原帖由 "lucky_han"]晕,没有做出来。要求一个在速度上优化,一个在内存占用上优化。[/quote 发表:

在内存上优化估计就是AERO说的方法了。
在速度上,嘿嘿,我有个歪主意:做数组,256个,嘿嘿。
作者: aero    时间: 2005-03-16 22:04
标题: 笔试题:如何求一个char中有几个1?
原帖由 "assiss" 发表:

在内存上优化估计就是AERO说的方法了。
在速度上,嘿嘿,我有个歪主意:做数组,256个,嘿嘿。


哈哈,好主意~!   
作者: lifyman    时间: 2005-03-16 22:11
标题: 笔试题:如何求一个char中有几个1?
用数组具体是怎么做的?
作者: aero    时间: 2005-03-16 22:16
标题: 笔试题:如何求一个char中有几个1?
手工计算数组,0号放0,1号放1,2号放1,3号放2,4号放1,5号放2……等等。然后直接取,比如要判断char ch;,直接取buf[ch],就要要的答案。
作者: bleem1998    时间: 2005-03-17 09:00
标题: 笔试题:如何求一个char中有几个1?
不懂
作者: unicorns    时间: 2005-03-17 10:28
标题: 笔试题:如何求一个char中有几个1?
就是用人脑代替电脑
事先把所有的情况算出来保存在数组里.

传进来的char是几就取几就是了.
反正一共256种可能,呵呵.
作者: 精简指令    时间: 2005-03-17 10:31
标题: 笔试题:如何求一个char中有几个1?
HOHO,这个做索引的方法,应该速度最快了。

是不是最近是招聘高峰期?都是试题……
作者: unicorns    时间: 2005-03-17 10:42
标题: 笔试题:如何求一个char中有几个1?
而且最近这类的题特别多.
作者: 精简指令    时间: 2005-03-17 10:45
标题: 笔试题:如何求一个char中有几个1?
应该把所有的面试题都归纳起来,看看都涉及什么技术点和常见的表现形式。
然后做个固定帖子,一劳永逸了^^
作者: aero    时间: 2005-03-17 11:08
标题: 笔试题:如何求一个char中有几个1?
原帖由 "精简指令" 发表:
应该把所有的面试题都归纳起来,看看都涉及什么技术点和常见的表现形式。
然后做个固定帖子,一劳永逸了^^


Good idea! And it will help us to prepare interview next time.
作者: hhlcjcj    时间: 2005-03-17 11:36
标题: 笔试题:如何求一个char中有几个1?
是啊,请版主多费心啊!!
作者: leizisdu    时间: 2011-10-26 16:06
回复 2# aero

作者: leizisdu    时间: 2011-10-26 16:07
回复 3# assiss

作者: 塑料袋    时间: 2011-10-26 16:49
以上都是瞎扯蛋,这个题目可以达到速度和内存都最少,在kernel的hweight32()中有个标准的算法。
作者: prc    时间: 2011-10-26 17:27
回复 16# 塑料袋

这是不可能发生的
作者: 塑料袋    时间: 2011-10-26 17:30
回复  塑料袋

这是不可能发生的
prc 发表于 2011-10-26 17:27



    为什么??
  
  3次移位后屏蔽,3次累加就可以啊
作者: zylthinking    时间: 2011-10-26 17:37
本来想帖 hweight的, 结果那个算法没看懂, 忘记它是找几个1还是找最近的一个1了, 就没敢帖

  1. unsigned int hweight32(unsigned int w)
  2. {
  3.     unsigned int res = w - ((w >> 1) & 0x55555555);
  4.     res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
  5.     res = (res + (res >> 4)) & 0x0F0F0F0F;
  6.     res = res + (res >> 8);
  7.     return (res + (res >> 16)) & 0x000000FF;
  8. }


  9. #define BITMAP_LAST_WORD_MASK(nbits)                    \
  10. (                                    \
  11.     ((nbits) % BITS_PER_LONG) ?                    \
  12.         (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL        \
  13. )

  14. int __bitmap_weight(const unsigned long *bitmap, int bits)
  15. {
  16.     int k, w = 0, lim = bits/BITS_PER_LONG;

  17.     for (k = 0; k < lim; k++)
  18.         w += hweight32(bitmap[k]);

  19.     if (bits % BITS_PER_LONG)
  20.         w += hweight32(bitmap[k] & BITMAP_LAST_WORD_MASK(bits));

  21.     return w;
  22. }
复制代码

作者: noword2k    时间: 2011-10-26 18:00
http://graphics.stanford.edu/~seander/bithacks.html
看那个算zero bits的。
作者: 塑料袋    时间: 2011-10-26 19:23
本来想帖 hweight的, 结果那个算法没看懂, 忘记它是找几个1还是找最近的一个1了, 就没敢帖
zylthinking 发表于 2011-10-26 17:37



    这啥不懂的?俺给你解释
作者: denghaipeng    时间: 2011-10-27 10:01
本帖最后由 denghaipeng 于 2011-10-27 10:14 编辑

char Onecount(char a)
{
    char c = 0;
    while (a = a & (a - 1))
    {
         c++;
    }
    return c;
}
作者: gosapphire    时间: 2011-10-27 10:37
用内存 就查表 就是上面兄弟说的 内存
用速度 就一个bit一个bit的&
作者: prc    时间: 2011-10-27 10:42
回复 18# 塑料袋


    3次移位,3次mask,3次累加

显然没有直接 return data[c]; 快

所以,你不可能达到时间和空间上同时最优
作者: zylthinking    时间: 2011-10-27 12:04
回复  塑料袋


    3次移位,3次mask,3次累加

显然没有直接 return data[c]; 快

所以,你不可能 ...
prc 发表于 2011-10-27 10:42


这个早有人做了比对了:
http://bbs.chinaunix.net/thread-795048-1-2.html
作者: prc    时间: 2011-10-27 13:47
回复 25# zylthinking


    那个讨论的是32位整数;本楼讨论的是8位整数。
由于构造一个包含2^32个元素的代价太大,不可能存在实用性。四次查表的折衷方案是有可能败于精心设计的算法。
但是就本楼而言,256个元素的数组是一个合理的可承受的代价。我真心不认为array[ i ] 这样一个简单的寻址/取数操作性能会比若干次shift,and操作会来的更慢。
作者: zylthinking    时间: 2011-10-27 14:10
回复  zylthinking


    那个讨论的是32位整数;本楼讨论的是8位整数。
由于构造一个包含2^32个元素的 ...
prc 发表于 2011-10-27 13:47


你是对的
作者: 塑料袋    时间: 2011-10-27 14:49
回复  zylthinking

那个讨论的是32位整数;本楼讨论的是8位整数。
由于构造一个包含2^32个元素的代价太大,不可能存在实用性。四次查表的折衷方案是有可能败于精心设计的算法。
但是就本楼而言,256个元素的数组是一个合理的可承受的代价。我真心不认为array[ i ] 这样一个简单的寻址/取数操作性能会比若干次shift,and操作会来的更慢。prc 发表于 2011-10-27 13:47


还真有可能更慢


因为它那个移位累加的计算,可以使用寄存器变量,整个函数完全用寄存器和立即数。

写256大小的数组的话,涉及到访存。


访一下内存的话,搞不好上百条指令都跑出去了。
作者: prc    时间: 2011-10-27 15:23
那我得提醒你一下:CPU读取指令本身也是有可能访问内存的;而访问256数组也有可能不访问内存。因为有cache存在,而cache本身的行为是不可预测的。

抛掉这些架构相关的东西,显然是数组的方式是速度最快的
作者: stonemason    时间: 2011-10-28 17:19
回复 21# 塑料袋


    看这里:http://bbs.chinaunix.net/thread-244216-2-1.html
再来个
  1. /*
  2.   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
  3.   Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
  4.   Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad

  5.   Stockfish is free software: you can redistribute it and/or modify
  6.   it under the terms of the GNU General Public License as published by
  7.   the Free Software Foundation, either version 3 of the License, or
  8.   (at your option) any later version.


  9.   Stockfish is distributed in the hope that it will be useful,
  10.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.   GNU General Public License for more details.

  13.   You should have received a copy of the GNU General Public License
  14.   along with this program.  If not, see <http://www.gnu.org/licenses/>.
  15. */

  16. #if !defined(BITCOUNT_H_INCLUDED)
  17. #define BITCOUNT_H_INCLUDED

  18. #include "types.h"

  19. enum BitCountType {
  20.     CNT64,
  21.     CNT64_MAX15,
  22.     CNT32,
  23.     CNT32_MAX15,
  24.     CNT_POPCNT
  25. };

  26. /// count_1s() counts the number of nonzero bits in a bitboard.
  27. /// We have different optimized versions according if platform
  28. /// is 32 or 64 bits, and to the maximum number of nonzero bits.
  29. /// We also support hardware popcnt instruction. See Readme.txt
  30. /// on how to pgo compile with popcnt support.
  31. template<BitCountType> inline int count_1s(Bitboard);

  32. template<>
  33. inline int count_1s<CNT64>(Bitboard b) {
  34.   b -= ((b>>1) & 0x5555555555555555ULL);
  35.   b = ((b>>2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL);
  36.   b = ((b>>4) + b) & 0x0F0F0F0F0F0F0F0FULL;
  37.   b *= 0x0101010101010101ULL;
  38.   return int(b >> 56);
  39. }

  40. template<>
  41. inline int count_1s<CNT64_MAX15>(Bitboard b) {
  42.   b -= (b>>1) & 0x5555555555555555ULL;
  43.   b = ((b>>2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL);
  44.   b *= 0x1111111111111111ULL;
  45.   return int(b >> 60);
  46. }

  47. template<>
  48. inline int count_1s<CNT32>(Bitboard b) {
  49.   unsigned w = unsigned(b >> 32), v = unsigned(b);
  50.   v -= (v >> 1) & 0x55555555; // 0-2 in 2 bits
  51.   w -= (w >> 1) & 0x55555555;
  52.   v = ((v >> 2) & 0x33333333) + (v & 0x33333333); // 0-4 in 4 bits
  53.   w = ((w >> 2) & 0x33333333) + (w & 0x33333333);
  54.   v = ((v >> 4) + v) & 0x0F0F0F0F; // 0-8 in 8 bits
  55.   v += (((w >> 4) + w) & 0x0F0F0F0F);  // 0-16 in 8 bits
  56.   v *= 0x01010101; // mul is fast on amd procs
  57.   return int(v >> 24);
  58. }

  59. template<>
  60. inline int count_1s<CNT32_MAX15>(Bitboard b) {
  61.   unsigned w = unsigned(b >> 32), v = unsigned(b);
  62.   v -= (v >> 1) & 0x55555555; // 0-2 in 2 bits
  63.   w -= (w >> 1) & 0x55555555;
  64.   v = ((v >> 2) & 0x33333333) + (v & 0x33333333); // 0-4 in 4 bits
  65.   w = ((w >> 2) & 0x33333333) + (w & 0x33333333);
  66.   v += w; // 0-8 in 4 bits
  67.   v *= 0x11111111;
  68.   return int(v >> 28);
  69. }

  70. template<>
  71. inline int count_1s<CNT_POPCNT>(Bitboard b) {
  72. #if !defined(USE_POPCNT)
  73.   return int(b != 0); // Avoid 'b not used' warning
  74. #elif defined(_MSC_VER) && defined(__INTEL_COMPILER)
  75.   return _mm_popcnt_u64(b);
  76. #elif defined(_MSC_VER)
  77.   return (int)__popcnt64(b);
  78. #elif defined(__GNUC__)
  79.   unsigned long ret;
  80.   __asm__("popcnt %1, %0" : "=r" (ret) : "r" (b));
  81.   return ret;
  82. #endif
  83. }

  84. #endif // !defined(BITCOUNT_H_INCLUDED)
复制代码

作者: 1053304571    时间: 2012-09-03 20:30
int   count(char  ch){
      char    num = 0;
      while(ch){
           num +=ch&1;
           ch >>= 1;
     }
}
作者: weiweiwang1988    时间: 2012-09-11 18:43
内存优化和速度优化分别是

内存优化:
//变量放在var
char var;

int i;
int cnt = 0;
for (i=1; i<=128; i<<=1)
    cnt += var&i;

速度优化:
char array[8] = {0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80};
int i = 0;
int cnt = 0;
for (i = 0; i<8; i++)
   cnt += array[i]&var;


作者: giant1st    时间: 2012-09-18 21:06
  1. while(value)
  2. {
  3.         value &= (value-1);
  4.         cnt++;
  5. }
复制代码

作者: sb_oy    时间: 2012-09-19 10:04
要么移位 + &  要么数组索引,数组索引的确有点占内存,其他的方法没想到

作者: ludm1187    时间: 2012-09-19 16:36
要速度 就用空间换时间
作者: ludm1187    时间: 2012-09-19 16:37
要省内存 就用速度换空间




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2