473,388 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

hmm, what does it do?

#define BLACKBOX(x) ((x)&((x)-1))

Jul 23 '05 #1
12 1785
On Mon, 21 Feb 2005 12:54:26 -0800, puzzlecracker wrote:
#define BLACKBOX(x) ((x)&((x)-1))


Some empirical analysis:

1 -> 0
2 -> 0
3 -> 2
4 -> 0
5 -> 4
6 -> 4
7 -> 6
8 -> 0
9 -> 8
10 -> 8
11 -> 10
12 -> 8
13 -> 12
14 -> 12
15 -> 14
16 -> 0

Looks like it could be a test for "power of 2" (result is 0 or non-zero).

- Jay
Jul 23 '05 #2
puzzlecracker wrote:
#define BLACKBOX(x) ((x)&((x)-1))


What does it do...? Not C++...Deprecated, I would say.

Anyway, you are the puzzle cracker !!! :-) Tell us...

Andrea
Jul 23 '05 #3
puzzlecracker wrote:
#define BLACKBOX(x) ((x)&((x)-1))


Perhaps it would help to get started if you thought of the output as a
boolean (i.e. paid attention only to zero vs. non-zero results).

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 23 '05 #4
int pwr2(int x); // Returns the highest power of 2
// that divides x

assert(BLACKBOX(x) == x - pwr2(x));

Jul 23 '05 #5
Yes!
BLACKBOX(x) return zero if and only if there is only one '1' in x, that is,
x is "power of 2"

"Jay Nabonne" <ja*********@sonicNOSPAM.com> ????
news:pa****************************@sonicNOSPAM.co m...
On Mon, 21 Feb 2005 12:54:26 -0800, puzzlecracker wrote:
#define BLACKBOX(x) ((x)&((x)-1))


Some empirical analysis:

1 -> 0
2 -> 0
3 -> 2
4 -> 0
5 -> 4
6 -> 4
7 -> 6
8 -> 0
9 -> 8
10 -> 8
11 -> 10
12 -> 8
13 -> 12
14 -> 12
15 -> 14
16 -> 0

Looks like it could be a test for "power of 2" (result is 0 or non-zero).

- Jay

Jul 23 '05 #6
"puzzlecracker" <ir*********@gmail.com> schrieb im Newsbeitrag news:11**********************@l41g2000cwc.googlegr oups.com...
#define BLACKBOX(x) ((x)&((x)-1))


That's part of a piece of code made famous by Edsgar Dijkstra. Translated to C++ it looks like this

int foo(int x)
{
int n = 0;
while (x)
{
++n;
x &= x-1;
}
return n;
}

Now guess what this one does. If you can solve one, you can probably solve the other, too.

Heinz
Jul 23 '05 #7
Yes!
BLACKBOX(x) return zero if and only if there is only one '1' in x, that is,
x is "power of 2"

"Jay Nabonne" <ja*********@sonicNOSPAM.com> ????
news:pa****************************@sonicNOSPAM.co m...
On Mon, 21 Feb 2005 12:54:26 -0800, puzzlecracker wrote:
#define BLACKBOX(x) ((x)&((x)-1))


Some empirical analysis:

1 -> 0
2 -> 0
3 -> 2
4 -> 0
5 -> 4
6 -> 4
7 -> 6
8 -> 0
9 -> 8
10 -> 8
11 -> 10
12 -> 8
13 -> 12
14 -> 12
15 -> 14
16 -> 0

Looks like it could be a test for "power of 2" (result is 0 or non-zero).

- Jay


Jul 23 '05 #8
On Tue, 22 Feb 2005 16:56:08 +0800, Pan Jiaming
<ja**@mails.tsinghua.edu.cn> wrote:
Yes!
BLACKBOX(x) return zero if and only if there is only one '1' in x, that is,
x is "power of 2"


a) Please don't top-post, write your answer below the quoted text (and
trim that to just the part you are answering).

b) Since when has zero been a power of 2 (BLACKBOX(0) is zero as well)?

c) Look at what it does in binary:

00000 & 11111 -> 00000
00001 & 00000 -> 00000
00010 & 00001 -> 00000
00011 & 00010 -> 00010
00100 & 00011 -> 00000
00101 & 00100 -> 00100
00110 & 00101 -> 00100
00111 & 00110 -> 00110
01000 & 00111 -> 00000
01001 & 01000 -> 01000
01010 & 01001 -> 01000
01011 & 01010 -> 01010
01100 & 01011 -> 01000
01101 & 01100 -> 01100
01110 & 01101 -> 01100
01111 & 01110 -> 01110
10000 & 01111 -> 00000

Now, what is it doing?

Chris C
Jul 23 '05 #9
This is one question in Microsoft ATC recruiting test in China this year.

"Heinz Ozwirk" <ho**********@arcor.de> ????
news:42***********************@newsread4.arcor-online.net...
"puzzlecracker" <ir*********@gmail.com> schrieb im Newsbeitrag
news:11**********************@l41g2000cwc.googlegr oups.com...
#define BLACKBOX(x) ((x)&((x)-1))


That's part of a piece of code made famous by Edsgar Dijkstra. Translated to
C++ it looks like this

int foo(int x)
{
int n = 0;
while (x)
{
++n;
x &= x-1;
}
return n;
}

Now guess what this one does. If you can solve one, you can probably solve
the other, too.

Heinz
Jul 23 '05 #10

"Heinz Ozwirk" <ho**********@arcor.de> wrote in message
news:421afabc$0$13221That's part of
Now guess what this one does. If you can solve one, you can probably solve
the other, too.


Counts the number of 1s in the binary expression of the number?
Jul 23 '05 #11

"puzzlecracker" <ir*********@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
#define BLACKBOX(x) ((x)&((x)-1))


It defines a macro called BLACKBOX with a parameter x, which expands as a
bitwise AND of x with x-1.

What do I win? :-)

Actually, the above is only strictly true for the built-in numeric types.
For a user-defined class, I believe it expands as

x.operator&(x.operator-(1))

(I'm terrible at this crap. But I'm correct, right?)

So what it "does" depends largely upon what the type of x is, and how the
operators & and - for that type are defined.

-Howard


Jul 23 '05 #12
"foo" counts the number of 1's in the binary representation.
The original problem turns the right-most 1 to a 0.

Jul 23 '05 #13

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

Similar topics

7
by: Jonas | last post by:
This works fine in Win XP but does not work at all in Win 98. Private WithEvents objIExplorer As InternetExplorer I have to do it like this to get it to work in Win 98 Dim objIExplorer As...
3
by: Jukka K. Korpela | last post by:
I have noticed that the meaning of visibility: collapse has been discussed on different forums, but with no consensus on what it really means. Besides, implementations differ. The specification...
11
by: Grant Edwards | last post by:
I've read over and over that Python leaves floating point issues up to the underlying platform. This seems to be largely true, but not always. My underlying platform (IA32 Linux) correctly...
0
by: Dirk Försterling | last post by:
Hi all, a few days ago, I upgraded from PostgreSQL 7.2.1 to 7.4, following the instructions in the INSTALL file, including dump and restore. All this worked fine without any error (message). ...
12
by: Frank Hauptlorenz | last post by:
Hello Out there! I have a DB2 V7.2 Database (Fix11) on Win 2000 Professional. It was before a NT 4 based Domain - now it is a Win 2000 Domain. The database server is a domain member. Now...
1
by: Georg Scholz | last post by:
Hello, The class "Control" contains a documented Property "ControlType". So for example, in a form, you can write code like this: Dim c as control set c = me.Controls("textbox1") if...
5
by: Genboy | last post by:
My "VIS" Website, which is a C# site created in VS.NET, Framework 1.1, is no longer compiling for me via the command line. As I have done 600 times in the last year and a half, I can compile to...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
16
by: lawrence k | last post by:
I've a file upload script on my site. I just now used it to upload a small text document (10k). Everything worked fine. Then I tried to upload a 5.3 meg Quicktime video. Didn't work. I've...
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...

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.