整数的1 [英] number of 1's in an integer

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

问题描述

有人可以建议我一个有效的计算方式

no。 1'的整数(假设int = 4的大小)?

注意:不允许循环。


i同意这是一个家庭作业问题。我试了很多

但是找不到任何解决方案。

任何提示都会受到高度赞赏。


thanx a吨...

Can anybody suggest me an efficient way of counting
no. of 1''s in an integer (assume size of int=4)?
Note: no looping is allowed.

i agree this is a homework question. i tried a lot
but couldn''t find any solution.
Any hints will be highly appreciated.

thanx a ton...

推荐答案

junky_fellow写道:
junky_fellow wrote:
任何人都可以建议我一个有效的方式[。 ..]
Can anybody suggest me an efficient way of [...]




您对高效的定义是什么?


执行时间短?代码小吗?数据量小?易于阅读?


顺便说一句,没有必要假设int中有多少位,你可以使用sizeof(unsigned int)* b $ b CHAR_BIT



What is your definition of efficient?

Small execution time? Small code size? Small data size? Easy to read?

BTW, there is no need to assume how many bits there are in an int, you
can use sizeof(unsigned int) * CHAR_BIT




" junky_fellow" <菊********** @ yahoo.co.in>在消息中写道

news:8c ************************** @ posting.google.c om ...

"junky_fellow" <ju**********@yahoo.co.in> wrote in message
news:8c**************************@posting.google.c om...
任何人都可以建议我一个有效的计算方法
没有。 1'的整数(假设int = 4的大小)?
注意:不允许循环。

我同意这是一个功课问题。我尝试了很多
但是找不到任何解决方案。
任何提示都将受到高度赞赏。

非常好......
Can anybody suggest me an efficient way of counting
no. of 1''s in an integer (assume size of int=4)?
Note: no looping is allowed.

i agree this is a homework question. i tried a lot
but couldn''t find any solution.
Any hints will be highly appreciated.

thanx a ton...




创建一个包含256个条目的查找表,其中每个条目保存每个字节值(从0到255)的

位数:


/ *注意递归系列模式... * /

/ *检查前四个条目,然后是每个其他条目,然后每个

第四个条目(见模式。?。)* /

/ *另一种解决方案可以利用这种模式.... !!! * /

int bits_in_a_byte [256] = {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1 ,2,2,3,

2,3,3,4,2,3,3,4,3,4,4,5.etc ..};


对于你4字节int中的每个字节,你可以在这个表上查找和

将所有结果加在一起。


您可以使用指针的转换来获取int的每个字节,如下所示:


long值;

char byte1,byte2,byte3,byte4;

char * p;


p =(char *)& value;

byte1 = p [0];

byte2 = p [1];

byte3 = p [2];

byte4 = p [3];


这对长度等等做了一些假设,但我希望你得到

一般要点...


Sean




Create a lookup table with 256 entries where each entry holds the number of
bits for each byte value (from 0 - 255):

/* Notice the recursive series pattern... */
/* Check out the first four entries, then every other entry, then every
fourth entry (see the pattern.?.) */
/* Another solution could make use of this pattern....!!! */
int bits_in_a_byte[256]={0,1,1,2, 1,2,2,3, 1,2,2,3, 2,3,3,4, 1,2,2,3 ,
2,3,3,4, 2,3,3,4, 3,4,4,5.etc..};

The for each byte in you 4 byte int you can do a lookup on this table and
add all the results together.

You can use casting of pointers to get each byte of the int as follows:

long value;
char byte1, byte2, byte3, byte4;
char *p;

p=(char *) &value;
byte1= p[0];
byte2=p[1];
byte3=p[2];
byte4=p[3];

This makes some assumptions about the sizeof long etc but I hope you get the
general gist...

Sean





junky_fellow写道:


junky_fellow wrote:
有人能建议我一个有效的计算方法吗?没有。 1'的整数(假设int = 4的大小)?
注意:不允许循环。

我同意这是一个功课问题。我尝试了很多
但是找不到任何解决方案。
任何提示都将受到高度赞赏。

非常好......
Can anybody suggest me an efficient way of counting
no. of 1''s in an integer (assume size of int=4)?
Note: no looping is allowed.

i agree this is a homework question. i tried a lot
but couldn''t find any solution.
Any hints will be highly appreciated.

thanx a ton...




因为你说这是一个功课问题我不会给你代码

但建议你使用递归来解决循环比特问题。并且

当你要移动你的整数时要小心:移动

右边有符号的int是实现定义的。但是,如果你做对了,

你甚至不需要知道(更不用说假设)

你的整数。


-

ir。 H.J.H.N. Kenter ^^

电子设计&工具oo)飞利浦研究实验室

建筑方式3.23 = x = \ ar *** *******@philips.com

Holstlaan教授4(WAY31)| \电话。 +31 40 27 45334

5656 AA埃因霍温/ | __ \ tfx。 +31 40 27 44626

荷兰(____)_ / http ://www.kenter.demon.nl/


着名的句子:分段错误(核心倾倒)



Since you say it''s a homework question I won''t give you the code
but suggest you use recursion to work around the looping bit. And
take care when you are going to shift your integer number: shifting
right signed ints is implementation defined. But if you get it right,
you don''t even need to know (not to mention assume) the size of
your ints.

--
ir. H.J.H.N. Kenter ^^
Electronic Design & Tools oo ) Philips Research Labs
Building WAY 3.23 =x= \ ar**********@philips.com
Prof. Holstlaan 4 (WAY31) | \ tel. +31 40 27 45334
5656 AA Eindhoven /|__ \ tfx. +31 40 27 44626
The Netherlands (____)_/ http://www.kenter.demon.nl/

Famous last words: Segmentation Fault (core dumped)


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

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