a bit of a puzzle | Bytes (2024)

Steven G. Johnson

Here is a little algorithm I came across whose implementation is
amusingly obscure: what simple function does the following C code
compute, and why?

#include <stdint.h>
unsigned foo(uint32_t n)
{
const uint32_t a = 0x05f66a47;
static const unsigned bar[32] =
{0,1,2,26,23,3, 15,27,24,21,19, 4,12,16,28,6,31 ,25,22,14,20,18 ,11,5,30,13,17, 10,29,9,8,7};
n = ~n;
return bar[(a * (n & (-n))) >27];
}

To save you the trouble of compiling and running it yourself, here is
what it produces for n = 0,1,2,...,31:

0 -0, 1 -1, 2 -0, 3 -2, 4 -0, 5 -1, 6 -0, 7 -3, 8 ->
0, 9 -1, 10 -0, 11 -2, 12 -0, 13 -1, 14 -0, 15 -4, 16 ->
0, 17 -1, 18 -0, 19 -2, 20 -0, 21 -1, 22 -0, 23 -3, 24 -

0, 25 -1, 26 -0, 27 -2, 28 -0, 29 -1, 30 -0, 31 -5

Mar 9 '08 #1

Subscribe Reply

7 a bit of a puzzle | Bytes (1) 1878 a bit of a puzzle | Bytes (2)

CBFalconer

"Steven G. Johnson" wrote:

>
Here is a little algorithm I came across whose implementation is
amusingly obscure: what simple function does the following C code
compute, and why?

#include <stdint.h>
unsigned foo(uint32_t n)
{
const uint32_t a = 0x05f66a47;
static const unsigned bar[32] =
{0,1,2,26,23,3, 15,27,24,21,19, 4,12,16,28,6,31 ,25,22,14,20,18 ,11,5,30,13,17, 10,29,9,8,7};
n = ~n;
return bar[(a * (n & (-n))) >27];
}

Doesn't seem to work very well on a machine with 16 bit integers.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Mar 10 '08 #2

Keith Thompson

CBFalconer <cb********@yah oo.comwrites:

"Steven G. Johnson" wrote:
>Here is a little algorithm I came across whose implementation is
amusingly obscure: what simple function does the following C code
compute, and why?

#include <stdint.h>
unsigned foo(uint32_t n)
{
const uint32_t a = 0x05f66a47;
static const unsigned bar[32] =
{0,1,2,26,23,3 ,15,27,24,21,19 ,4,12,16,28,6,3 1,25,22,14,20,1 8,11,5,30,13,17 ,10,29,9,8,7};
n = ~n;
return bar[(a * (n & (-n))) >27];
}


Doesn't seem to work very well on a machine with 16 bit integers.

Most machines have 16-bit integers; often the 16-bit integer type is
called "short".

If you mean 16-bit ints, I don't see how that would cause a problem,
since the fucntion's argument is of type uint32_t. (The unsigned
result shouldn't be a problem unless you're worried about integers
with more than 32767 bits.)

Or am I missing something?

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Mar 10 '08 #3

Joachim Schmitz

CBFalconer wrote:

"Steven G. Johnson" wrote:
>>
Here is a little algorithm I came across whose implementation is
amusingly obscure: what simple function does the following C code
compute, and why?

#include <stdint.h>
unsigned foo(uint32_t n)
{
const uint32_t a = 0x05f66a47;
static const unsigned bar[32] =
{0,1,2,26,23,3 ,15,27,24,21,19 ,4,12,16,28,6,3 1,25,22,14,20,1 8,11,5,30,13,17 ,10,29,9,8,7};
n = ~n;
return bar[(a * (n & (-n))) >27];
}


Doesn't seem to work very well on a machine with 16 bit integers.

Mind to enlighten me why?

Bye, Jojo

Mar 10 '08 #4

user923005

On Mar 9, 10:40*am, "Steven G. Johnson" <stev...@alum.m it.eduwrote:

Here is a little algorithm I came across whose implementation is
amusingly obscure: what simple function does the following C code
compute, and why?

#include <stdint.h>
unsigned foo(uint32_t n)
{
* * *const uint32_t a = 0x05f66a47;
* * *static const unsigned bar[32] =
{0,1,2,26,23,3, 15,27,24,21,19, 4,12,16,28,6,31 ,25,22,14,20,18 ,11,5,30,13,17, *10,29,9,8,7};
* * *n = ~n;
* * *return bar[(a * (n & (-n))) >27];

}

To save you the trouble of compiling and running it yourself, here is
what it produces for n = 0,1,2,...,31:

0 -0, 1 -1, 2 -0, 3 -2, 4 -0, 5 -1, 6 -0, 7 -3, 8 ->
0, 9 -1, 10 -0, 11 -2, 12 -0, 13 -1, 14 -0, 15 -4, 16 ->
0, 17 -1, 18 -0, 19 -2, 20 -0, 21 -1, 22 -0, 23 -3, 24 -

0, 25 -1, 26 -0, 27 -2, 28 -0, 29 -1, 30 -0, 31 -5- Hide quoted text -

Here's a 64 bit version of something very similar:
const int lsz64_tbl[64] =
{
0, 31, 4, 33, 60, 15, 12, 34,
61, 25, 51, 10, 56, 20, 22, 35,
62, 30, 3, 54, 52, 24, 42, 19,
57, 29, 2, 44, 47, 28, 1, 36,
63, 32, 59, 5, 6, 50, 55, 7,
16, 53, 13, 41, 8, 43, 46, 17,
26, 58, 49, 14, 11, 40, 9, 45,
21, 48, 39, 23, 18, 38, 37, 27,
};
//Gerd Isenberg's implementation of bitscan:
int GerdBitScan(Bit board bb)
{
const Bitboard lsb = (bb & -(long long) bb) - 1;
const unsigned int foldedLSB = ((int) lsb) ^ ((int) (lsb >32));
return lsz64_tbl[foldedLSB * 0x78291ACF >26];
}

//Gerd Isenberg's implementation of bitscan with clear:
int GerdBitScanRese t(Bitboard *bb)
{
const Bitboard lsb = (bb[0] & -(long long) bb[0]) - 1;
const unsigned int foldedLSB = ((int) lsb) ^ ((int) (lsb >32));
bb[0] &= (bb[0] - 1);
return lsz64_tbl[foldedLSB * 0x78291ACF >26];
}

Chess programmers will recognize DeBrun's sequences. It was
popularized by Matthew Henry, IIRC.
See, for instance:
http://chessprogramming.wikispaces.com/BitScan

Mar 10 '08 #5

user923005

On Mar 10, 3:02*pm, user923005 <dcor...@connx. comwrote:

On Mar 9, 10:40*am, "Steven G. Johnson" <stev...@alum.m it.eduwrote:
Here is a little algorithm I came across whose implementation is
amusingly obscure: what simple function does the following C code
compute, and why?
#include <stdint.h>
unsigned foo(uint32_t n)
{
* * *const uint32_t a = 0x05f66a47;
* * *static const unsigned bar[32] =
{0,1,2,26,23,3, 15,27,24,21,19, 4,12,16,28,6,31 ,25,22,14,20,18 ,11,5,30,13,17, **10,29,9,8,7};
* * *n = ~n;
* * *return bar[(a * (n & (-n))) >27];
}
To save you the trouble of compiling and running it yourself, here is
what it produces for n = 0,1,2,...,31:
0 -0, 1 -1, 2 -0, 3 -2, 4 -0, 5 -1, 6 -0, 7 -3, 8 ->
0, 9 -1, 10 -0, 11 -2, 12 -0, 13 -1, 14 -0, 15 -4, 16 ->
0, 17 -1, 18 -0, 19 -2, 20 -0, 21 -1, 22 -0, 23 -3, 24 -
0, 25 -1, 26 -0, 27 -2, 28 -0, 29 -1, 30 -0, 31 -5- Hidequoted text -

Here's a 64 bit version of something very similar:
const int * * * lsz64_tbl[64] =
{
* * 0, 31, 4, 33, 60, 15, 12, 34,
* * 61, 25, 51, 10, 56, 20, 22, 35,
* * 62, 30, 3, 54, 52, 24, 42, 19,
* * 57, 29, 2, 44, 47, 28, 1, 36,
* * 63, 32, 59, 5, 6, 50, 55, 7,
* * 16, 53, 13, 41, 8, 43, 46, 17,
* * 26, 58, 49, 14, 11, 40, 9, 45,
* * 21, 48, 39, 23, 18, 38, 37, 27,};

//Gerd Isenberg's implementation of bitscan:
int * * * * * * GerdBitScan(Bit board bb)
{
* * const Bitboard *lsb = (bb & -(long long) bb) - 1;
* * const unsigned int foldedLSB = ((int) lsb) ^ ((int) (lsb >32));
* * return lsz64_tbl[foldedLSB * 0x78291ACF >26];

}

//Gerd Isenberg's implementation of bitscan with clear:
int * * * * * * GerdBitScanRese t(Bitboard *bb)
{
* * const Bitboard *lsb = (bb[0] & -(long long) bb[0]) - 1;
* * const unsigned int foldedLSB = ((int) lsb) ^ ((int) (lsb >32));
* * bb[0] &= (bb[0] - 1);
* * return lsz64_tbl[foldedLSB * 0x78291ACF >26];

}

Chess programmers will recognize DeBrun's sequences. *It was
popularized by Matthew Henry, IIRC.
See, for instance:http://chessprogramming.wikispaces.com/BitScan- Hide quoted text -

- Show quoted text -

Oops... Left out the typedef necessary to grok this code:
typedef unsigned long long Bitboard;

Mar 10 '08 #6

Richard

CBFalconer <cb********@yah oo.comwrites:

"Steven G. Johnson" wrote:
>>
Here is a little algorithm I came across whose implementation is
amusingly obscure: what simple function does the following C code
compute, and why?

#include <stdint.h>
unsigned foo(uint32_t n)
{
const uint32_t a = 0x05f66a47;
static const unsigned bar[32] =
{0,1,2,26,23,3 ,15,27,24,21,19 ,4,12,16,28,6,3 1,25,22,14,20,1 8,11,5,30,13,17 ,10,29,9,8,7};
n = ~n;
return bar[(a * (n & (-n))) >27];
}


Doesn't seem to work very well on a machine with 16 bit integers.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.

Why? Please explain.

Mar 10 '08 #7

CBFalconer

Keith Thompson wrote:

CBFalconer <cb********@yah oo.comwrites:
>"Steven G. Johnson" wrote:
>>Here is a little algorithm I came across whose implementation is
amusingly obscure: what simple function does the following C code
compute, and why?

#include <stdint.h>
unsigned foo(uint32_t n) {
const uint32_t a = 0x05f66a47;
static const unsigned bar[32] =
{0,1,2,26,23, 3,15,27,24,21,1 9,4,12,16,28,6, 31,25,22,14,20, 18,11,5,30,13,1 7,10,29,9,8,7};
n = ~n;
return bar[(a * (n & (-n))) >27];
}


Doesn't seem to work very well on a machine with 16 bit integers.

Most machines have 16-bit integers; often the 16-bit integer type is
called "short".

If you mean 16-bit ints, I don't see how that would cause a problem,
since the fucntion's argument is of type uint32_t. (The unsigned
result shouldn't be a problem unless you're worried about integers
with more than 32767 bits.)

Or am I missing something?

Yeah, I meant int, and was sloppy. To me, uint32_t doesn't exist,
since it is not guaranteed. Lets make the complaint about a
machine with an 18 bit int.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Mar 13 '08 #8

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

Similar topics

1 6096

sliding puzzle code help

by: Developwebsites |last post by:

Hi all, I've made a sliding puzzle game in shockwave which works just fine, except I dont know how to have it solve itself. the URL is: http://members.aol.com/rglukov/games/selfsolve.htm There are lots of these on the net, made in java, flash, C++, javascript, etc. and as shareware, but none seem to be solving the puzzle by

C / C++

42 2957

Challenge: Triangles puzzle

by: Frank Buss |last post by:

I've setup a challenge, mainly for C++, Java and Lisp, but every other language is welcome: http://www.frank-buss.de/challenge/index.html There is nothing to win, but I hope there will be some interesting solutions at the end, so the win are the results :-) -- Frank Buß, fb@frank-buss.de

C / C++

1 13084

cross word puzzle java program

by: xavier vazquez |last post by:

I have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of the other....how i can fix this the program look like this import java.util.ArrayList; import java.util.Random;

Java

2010

cross word puzzle cont 2dn part

by: xavier vazquez |last post by:

have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of the other....how i can fix this this run the random words for the program import javax.swing.JOptionPane; import java.util.ArrayList; import java.util.Random; public class CrossWordPuzzleTester {

Java

5 4459

Puzzle program

by: ashish0799 |last post by:

HI I M ASHISH I WANT ALGORYTHMUS OF THIS PROBLEM Jigsaw puzzles. You would have solved many in your childhood and many people still like it in their old ages also. Now what you have got to do is to solve jigsaw puzzles using the computer. The jigsaw puzzle here is a square of dimension d (a puzzle with d^2 pieces) and the jigsaw pieces (all same dimensions) are of dimensions H x W (Which means the pieces have ‘H’ rows of ‘W’...

C / C++

3 3200

How could i write a puzzle program?

by: oncue01 |last post by:

Word Puzzle Task You are going to search M words in an N × N puzzle. The words may have been placed in one of the four directions as from (i) left to right (E), (ii) right to left (W), (iii) up to bottom (S), or (iv) bottom to up (N). The program will print the starting place and the direction of each word. Limitations The number of words to be searched can be at most 100, the size of the puzzle N can be minimum 5 maximum 20....

C / C++

6 2569

RFC - n-puzzle.py

by: Phoe6 |last post by:

Hi All, I would like to request a code and design review of one of my program. n-puzzle.py http://sarovar.org/snippet/detail.php?type=snippet&id=83 Its a N-puzzle problem solver ( Wikipedia page and http://norvig.com/ltd/test/n-puzzle.lisp ) I have used OO Python for the above program and would like comments on my approach as I am just starting with OOP.

Python

2 2692

by: Gio |last post by:

I'm getting K&R (it's on the way), should I also get the Answer Book? And while I'm there, should I get the Puzzle Book? Or should I save the Puzzle Book for when I'm more advanced? - Gio -- AND NOW FOR A WORD (an IF blog):

C / C++

4 19925

15 puzzle

by: honey777 |last post by:

Problem: 15 Puzzle This is a common puzzle with a 4x4 playing space with 15 tiles, numbered 1 through 15. One "spot" is always left blank. Here is an example of the puzzle: The goal is to get the tiles in order, 1 through 15, from left to right, top to bottom, by just sliding tiles into the empty square. In this configuration, the goal would be to get the 14 and 15 to switch places, without affecting any of the other squares. Your...

C / C++

5 4824

Javascript Puzzle Image Swap

by: dmf1207 |last post by:

Hi All! I'm new to javascript and need a little help with a simple puzzle im trying to design. I have a 600x100 pixel picture that I have sliced into 6 100x100 rectangles making a table of of 6 columns and 1 row. I wanted to make a "swap puzzle" where the user clicks on one image then the other and they swap. At first load i want to have the puzzle in a scrambled order and then when they complete it I want to alert the user with how many clicks...

Javascript

8175

What is ONU?

by: marktang |last post by:

ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...

General

8568

Maximizing Business Potential: The Nexus of Website Design and Digital Marketing

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...

Online Marketing

1 8263

The easy way to turn off automatic updates for Windows 10/11

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,...

Windows Server

8423

Discussion: How does Zigbee compare with other wireless protocols in smart home applications?

by: tracyyun |last post by:

Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...

General

7047

AI Job Threat for Devs

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...

Career Advice

1 6084

Access Europe - Using VBA to create a class based on a table - Wed 1 May

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...

Microsoft Access / VBA

4111

Windows Forms - .Net 8.0

by: adsilva |last post by:

A Windows Forms form does not have the event Unload, like VB6. What one acts like?

Visual Basic .NET

1 2560

transfer the data from one system to another through ip address

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

C# / C Sharp

1420

Comprehensive Guide to Website Development in Toronto: Expert Insights from BSMN Consultancy

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...

General

a bit of a puzzle | Bytes (2024)
Top Articles
Latest Posts
Article information

Author: Carlyn Walter

Last Updated:

Views: 5544

Rating: 5 / 5 (70 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Carlyn Walter

Birthday: 1996-01-03

Address: Suite 452 40815 Denyse Extensions, Sengermouth, OR 42374

Phone: +8501809515404

Job: Manufacturing Technician

Hobby: Table tennis, Archery, Vacation, Metal detecting, Yo-yoing, Crocheting, Creative writing

Introduction: My name is Carlyn Walter, I am a lively, glamorous, healthy, clean, powerful, calm, combative person who loves writing and wants to share my knowledge and understanding with you.