473,698 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Challenging BitManipulation puzzles...

Here is the problem.
The funtion "bitcount" has the parameter x( integer type, 4byte).
The "bitcount" should count the number of '1' from the binary
representation
of x.
But You shouldn't use either 'if' or 'else'. You're allowed to use
bitwise
operators such as '!, ~, ^, &, |, >>, <<'. You can use any local
variables,
constants, and '=' operator.
Preventing you from counting in brute-force manner such
as(x&1+x>>1&1+x >>2&1+...x>>30& 1+x>>31&1), You have to count the bit
number within 40 operators.
This is one of my assignment, but I can't find any solution. A little
hint
would be great help for me. Waiting for your reply. Thanks.
Nov 13 '05 #1
4 2547
In article <45************ **************@ posting.google. com>,
le****@lycos.co .kr (lefoot) wrote:
Here is the problem.
The funtion "bitcount" has the parameter x( integer type, 4byte).
The "bitcount" should count the number of '1' from the binary
representation
of x.
But You shouldn't use either 'if' or 'else'. You're allowed to use
bitwise
operators such as '!, ~, ^, &, |, >>, <<'. You can use any local
variables,
constants, and '=' operator.
Preventing you from counting in brute-force manner such
as(x&1+x>>1&1+x >>2&1+...x>>30& 1+x>>31&1), You have to count the bit
number within 40 operators.
This is one of my assignment, but I can't find any solution. A little
hint
would be great help for me. Waiting for your reply. Thanks.


Make a table with 2048 entries, where table [i] = number of bits in i.
Then calculate table [x >> 22] + table [(x >> 11) & 0x7ff] + table [x &
0x7ff]. On my Macintosh that is compiled into exactly eight assembler
instructions.

By the way, if you have a Macintosh you can also submit your original
brute-force method (but I would subtract points for mixing & + and >>
without brackets), because it uses only 31 assembler instructions :-)
Nov 13 '05 #2

On Tue, 29 Sep 2003, lefoot wrote:

Here is the problem.


[bitcount HW]

Google "HAKMEM". Cut and paste.
[Hint: "parallel addition."]
If you have a real C question, you
can ask it here.

-Arthur
Nov 13 '05 #3
le****@lycos.co .kr (lefoot) writes:
The funtion "bitcount" has the parameter x( integer type, 4byte).
The "bitcount" should count the number of '1' from the binary
representation
of x.
But You shouldn't use either 'if' or 'else'. You're allowed to use
bitwise
operators such as '!, ~, ^, &, |, >>, <<'. You can use any local
variables,
constants, and '=' operator.
Preventing you from counting in brute-force manner such
as(x&1+x>>1&1+x >>2&1+...x>>30& 1+x>>31&1), You have to count the bit
number within 40 operators.


Refer to section 5-1 of H.S. Warren, Jr., _Hacker's Delight_,
which has an extensive discussion of the "population count"
problem.
--
"What is appropriate for the master is not appropriate for the novice.
You must understand the Tao before transcending structure."
--The Tao of Programming
Nov 13 '05 #4
lefoot wrote:

Here is the problem.
The funtion "bitcount"


unsigned bit_count(unsig ned n)
{
unsigned count;

for (count = 0; n; n &= n - 1) {
++count;
}
return count;
}

--
pete
Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1654
by: Kevin Wan | last post by:
Hi, I have some puzzles about sgistl source code, that is why some methods are placed in protected field. To vector itself, private and protected are the same, and vector isn't desired to be the base class of other containers. So why not use private instead of protected?
17
1927
by: madhav_a_kelkar | last post by:
i was browsing this problem: The following is a piece of C code, whose intention was to print a minus sign 20 times. But you can notice that, it doesn't work. #include <stdio.h> int main() { int i; int n = 20;
11
1418
by: George | last post by:
Was my question too challenging for everyone? I thought I would get a much quicker response, complete with witty and (sometimes) condescending remarks Here's my question again, in case you missed it How do I reload a page Notice I did not ask "How do I *refresh* a page". I have no interest in the header meta refresh as this clears out the data I need to show. Also, Response.Redirect does me no good. It completely bypasses the data as...
18
1581
by: Frankie | last post by:
I have been hired to go to a former client of mine and train their staff programmers on ASP.NET. These guys have only Mainframe, MS Access, SQL Server, and VB6 desktop application development experience (with absolutely no HTML or Web application experience). Before jumping into any code I think it's important to get them to understand the fundamental differences and unique challenges presented by Web application development (independent...
2
1655
by: xPy | last post by:
do you know any links where i can find puzzles, like the 8 queens problem? (I'm not intrested in the solution, only for puzzle...)
11
2210
by: John Salerno | last post by:
Similar to the Python Challenge, does anyone know of any other websites or books that have programming puzzles to solve? I found a book called "Puzzles for Hackers", but it seems like it might be a little advanced for me, and I've also read that it focuses too much on encryption and security issues and doesn't really have coding problems, exactly. But something like that book would be fun to have.
5
1232
by: alainpoint | last post by:
Hi, I have what in my eyes seems a challenging problem. Thanks to Peter Otten, i got the following code to work. It is a sort of named tuple. from operator import itemgetter def constgetter(value): def get(self): return value return get def createTuple(*names):
2
5165
by: =?Utf-8?B?Q3JtTmV3Ymll?= | last post by:
Hi, 1) I want to hone my problem solving skills and be good at logic. How do I achieve this? 2) Where can I find c# puzzles or c# algorithms. 3) How do I print the values of a N X N matrix using c#. 1 2 3 4 5 6 7 8
25
5454
by: Oltmans | last post by:
Hi guys, I'm learning JavaScript and I need some puzzles that can make me a better JavaScript programmer. I mean I'm looking out for programming puzzles (e.g. Project Euler or TopCoder) but I'm looking out for language specific puzzles that can make me a top-notch JavaScript programmer. a) Any puzzles you can recommend? b) Any programs that you can suggest that can make me learn JavaScript internals in greatest depth. Please recommend...
62
12211
jkmyoung
by: jkmyoung | last post by:
Does anyone have some super, super hard Sudoku puzzles? Back in February this year, I had enough time to finally program a Sudoku solver in Java. Right now, I'm looking for solvable puzzles, but ones that my program cannot solve. However, most hard puzzles in the newspapers are solved within 2-3 cycles so far. The ones I've found on google which are said to be hard, get solved in 3 cycles. Any help would be very much appreciated!...
0
8610
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8902
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7740
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4623
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2339
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.