473,387 Members | 1,705 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,387 software developers and data experts.

Bit fiddling

I have got few questions on bit fiddling!!
1. Fast way of Multiplying a No by 7.
2. Counting no of set bits in a 32 bit no without looping.
3. finding id a no is divisible by 3 or not without using %, /, and -
operators,

May 28 '06 #1
21 4188
On 28 May 2006 05:30:10 -0700, "saurabhnsit2001"
<sa*************@gmail.com> wrote:
I have got few questions on bit fiddling!!


"I have got a few homework questions on bit fiddling"
May 28 '06 #2
saurabhnsit2001 said:
I have got few questions on bit fiddling!!
1. Fast way of Multiplying a No by 7.
n *= 7;

I have no complaints about the computer's speed when it comes to multiplying
by 7 - it can do this way faster than I can.
2. Counting no of set bits in a 32 bit no without looping.
nbits = 0;
if(x & 0x1) ++nbits;
if(x & 0x2) ++nbits;
if(x & 0x4) ++nbits;
if(x & 0x8) ++nbits;
if(x & 0x10) ++nbits;
if(x & 0x20) ++nbits;
if(x & 0x40) ++nbits;
if(x & 0x80) ++nbits;
if(x & 0x100) ++nbits;
if(x & 0x200) ++nbits;
if(x & 0x400) ++nbits;
if(x & 0x800) ++nbits;
if(x & 0x1000) ++nbits;
if(x & 0x2000) ++nbits;
if(x & 0x4000) ++nbits;
if(x & 0x8000) ++nbits;
if(x & 0x10000) ++nbits;
if(x & 0x20000) ++nbits;
if(x & 0x40000) ++nbits;
if(x & 0x80000) ++nbits;
if(x & 0x100000) ++nbits;
if(x & 0x200000) ++nbits;
if(x & 0x400000) ++nbits;
if(x & 0x800000) ++nbits;
if(x & 0x1000000) ++nbits;
if(x & 0x2000000) ++nbits;
if(x & 0x4000000) ++nbits;
if(x & 0x8000000) ++nbits;
if(x & 0x10000000) ++nbits;
if(x & 0x20000000) ++nbits;
if(x & 0x40000000) ++nbits;
if(x & 0x80000000) ++nbits;
3. finding id a no is divisible by 3 or not without using %, /, and -
operators,


printf("Is %d divisible by 3?\n", n);
if((ch = getchar()) == 'y' || ch == 'Y')
{
printf("%d is divisible by 3.\n", n);
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 28 '06 #3
saurabhnsit2001 wrote:
I have got few questions on bit fiddling!!
Wow! That is so exciting!! I can hardly contain
my enthusiasm!!!
1. Fast way of Multiplying a No by 7.
This is a trick question: There is no fast way to
multiply by seven. Multiplying by seven implies an
increase in magnitude, but magnitude decreases during
a fast.
2. Counting no of set bits in a 32 bit no without looping.
Another trick question: The double negative "no without"
implies that you *must* use a loop. If you submit a solution
that does not have a loop, you will receive a bad mark.
3. finding id a no is divisible by 3 or not without using %, /, and -
operators,


Your professor must be very fond of trick questions! In
this case you are asked about Freud's division of consciousness
into three parts (note "divisible by 3"): the ego, the id, and
the superego. This model is largely discredited by mainstream
psychiatrists, but is still taught as part of the history of the
discipline and still believed in by laypersons and a few fringe
operators.

How to find your id is a matter outside the C Standard.

--
Eric Sosman
es*****@acm-dot-org.invalid
May 28 '06 #4
saurabhnsit2001 wrote:
I have got few questions on bit fiddling!!
1. Fast way of Multiplying a No by 7.
2. Counting no of set bits in a 32 bit no without looping.


I like the number 3! You can use it to count bits!!

unsigned int nb(unsigned int n) {
return n>3? nb(n>>0x333/333)+nb(n<<3&033):((n+n+(3)/3)/3);
}
--
Thad
May 28 '06 #5
saurabhnsit2001 wrote:
I have got few questions on bit fiddling!!
1. Fast way of Multiplying a No by 7.
2. Counting no of set bits in a 32 bit no without looping.
3. finding id a no is divisible by 3 or not without using %, /, and -
operators,


You mean bit "twiddling".
May 30 '06 #6

saurabhnsit2001 wrote:
I have got few questions on bit fiddling!!
1. Fast way of Multiplying a No by 7.
2. Counting no of set bits in a 32 bit no without looping.
3. finding id a no is divisible by 3 or not without using %, /, and -
operators,


Ans For Q3 is

#include<stdio.h>
main()
{
int n;
scanf("%d",&n);
while(n>=3)
{
n=n+(~3)+1;
}
if(n==0)
printf("\n it IS divisible by 3");
else
printf("Not divisible By 3");
}

May 30 '06 #7
On 2006-05-30, ja********@gmail.com <ja********@gmail.com> wrote:

saurabhnsit2001 wrote:
I have got few questions on bit fiddling!!
1. Fast way of Multiplying a No by 7.
2. Counting no of set bits in a 32 bit no without looping.
3. finding id a no is divisible by 3 or not without using %, /, and -
operators,


Ans For Q3 is ...


Fixed version (mainly formatting):

#include <stdio.h>

int main(void)
{
int n;
scanf ("%d",&n);

while (n >= 3)
{
n += (~3);
n++;
}

if (n == 0)
printf ("\nThat number is divisible by 3\n");
else
printf ("\nThat number is not divisible By 3\n");

return 0;
}

Note that you declared main() wrong, you didn't put a \n at the end of
your printfs, you returned nothing from main, and you used either no
spacing, or tabs.

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
You can lead a blind man to water but you can't make him chug it.
May 30 '06 #8


Andrew Poelstra wrote On 05/30/06 09:56,:
On 2006-05-30, ja********@gmail.com <ja********@gmail.com> wrote:
saurabhnsit2001 wrote:
I have got few questions on bit fiddling!!
1. Fast way of Multiplying a No by 7.
2. Counting no of set bits in a 32 bit no without looping.
3. finding id a no is divisible by 3 or not without using %, /, and -
operators,


Ans For Q3 is ...

Fixed version (mainly formatting):

#include <stdio.h>

int main(void)
{
int n;
scanf ("%d",&n);

while (n >= 3)
{
n += (~3);
n++;
}

if (n == 0)
printf ("\nThat number is divisible by 3\n");
else
printf ("\nThat number is not divisible By 3\n");

return 0;
}


Why does it tell me that -42 is not divisible by 3?

--
Er*********@sun.com

May 30 '06 #9
On 2006-05-30, Eric Sosman <Er*********@sun.com> wrote:


Andrew Poelstra wrote On 05/30/06 09:56,:
On 2006-05-30, ja********@gmail.com <ja********@gmail.com> wrote:
saurabhnsit2001 wrote:

I have got few questions on bit fiddling!!
1. Fast way of Multiplying a No by 7.
2. Counting no of set bits in a 32 bit no without looping.
3. finding id a no is divisible by 3 or not without using %, /, and -
operators,

Ans For Q3 is ...

Fixed version (mainly formatting):

#include <stdio.h>

int main(void)
{
int n;
scanf ("%d",&n);

while (n >= 3)
{
n += (~3);
n++;
}

if (n == 0)
printf ("\nThat number is divisible by 3\n");
else
printf ("\nThat number is not divisible By 3\n");

return 0;
}


Why does it tell me that -42 is not divisible by 3?

Because it is adding ~3, and in general bit twiddling is undefined
when working with negative integers. I should have caught that.

int n; should be unsigned int n;, or a better solution would have
been to set n to abs(n) right before the while loop.

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
You can lead a blind man to water but you can't make him chug it.
May 30 '06 #10
Andrew Poelstra <ap*******@localhost.localdomain> writes:
On 2006-05-30, Eric Sosman <Er*********@sun.com> wrote:

[...]
Why does it tell me that -42 is not divisible by 3?

Because it is adding ~3, and in general bit twiddling is undefined
when working with negative integers. I should have caught that.

int n; should be unsigned int n;, or a better solution would have
been to set n to abs(n) right before the while loop.


And if n == INT_MIN?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 30 '06 #11
saurabhnsit2001 wrote:
I have got few questions on bit fiddling!!
Please send us your teachers e-mail address so you
don't have to bother sending your homework to him,
since doing your own homework is too hard for you.
1. Fast way of Multiplying a No by 7.
Be 7 times badder. "no! no! No! No! No! NO! NO!"
(you get the exclimation points for free)

No = No * 7;

Table lookup.
2. Counting no of set bits in a 32 bit no without looping.
Wow! A 32 bit "no"! Did that hurt?
The "set bits" are the small pieces of scenery
on the stage, but you don't tell us what you did to
get a "no!" from it. It should still only a single
no, no matter how many times you bite it.

Table lookup.
3. finding id a no is divisible by 3 or not without using %, /, and -
operators,


What's an 'id-a-no'? Or is it you can't even copy
your homework properly?

All numbers are divisable by three. Try it yourself:
(1/3 = 0.333.., 2/3 = 0.666..., etc.)

You can use the '\' operater in mbasic, or the 'div'
operator in pascal.

Table lookup.

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
May 30 '06 #12
On 2006-05-30, Keith Thompson <ks***@mib.org> wrote:
Andrew Poelstra <ap*******@localhost.localdomain> writes:
On 2006-05-30, Eric Sosman <Er*********@sun.com> wrote:

[...]
Why does it tell me that -42 is not divisible by 3?

Because it is adding ~3, and in general bit twiddling is undefined
when working with negative integers. I should have caught that.

int n; should be unsigned int n;, or a better solution would have
been to set n to abs(n) right before the while loop.


And if n == INT_MIN?

You would need to check for that, and then add three to ensure that you
don't overflow when you abs() it.

Any other special cases I should know about?

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
You can lead a blind man to water but you can't make him chug it.
May 30 '06 #13


Andrew Poelstra wrote On 05/30/06 17:36,:
On 2006-05-30, Keith Thompson <ks***@mib.org> wrote:
Andrew Poelstra <ap*******@localhost.localdomain> writes:
On 2006-05-30, Eric Sosman <Er*********@sun.com> wrote:


[...]
Why does it tell me that -42 is not divisible by 3?
Because it is adding ~3, and in general bit twiddling is undefined
when working with negative integers. I should have caught that.

int n; should be unsigned int n;, or a better solution would have
been to set n to abs(n) right before the while loop.


And if n == INT_MIN?


You would need to check for that, and then add three to ensure that you
don't overflow when you abs() it.

Any other special cases I should know about?


Only two that I can think of off-hand, both having
to do with issues of representation. Hint #1: Is the
absolute value of ~3 greater or less than a thousand?
Hint #2: Is the value of ~3 even or odd? (See section
6.2.6.2, paragraph 2).

The principal lesson is that it is usually silly to
use "bit fiddling" for tasks of the kind posed by the
O.P. (more likely, by the O.P.'s homework assignment).
As this thread demonstrates, such tasks can be trickier
than they might appear, and the "obvious" solutions have
pitfalls. Avoiding the pits can consume more execution
time than you save by avoiding the % or the * or whatever.

Let's try to imagine a scenario where speeding up a
divisibility-by-three test would make a useful improvement.
On the six-year-old machine sitting in front of me at the
moment, it takes about 0.172 seconds to test a million
numbers for divisibility by three using the % operator.
By resorting to bit fiddling I can cut the time to just
0.013 seconds, better than a thirteenfold speedup. Wow!

... but how much "Wow" is warranted? To save just one
second of the program's total execution time, I'd need to
use the faster method on about six and a half million
numbers. Six and a half million divisible-by-three tests,
and I save one, yes folks, a whopping ONE second. If it
took me, say, one minute to write the code and convince
myself I hadn't botched it, I'd break even after my program
had done three hundred eighty million divisibility tests --
until that moment, I'm running in the red.

How badly do you need three hundred eighty million
divisible-by-three tests?

Now, there can be circumstances where micro-optimization
of this sort is warranted. I maintain, though, that it is
NEVER warranted before convincing evidence has been found
that it is necessary.

--
Er*********@sun.com

May 30 '06 #14
In article <sl**********************@localhost.localdomain> ,
Andrew Poelstra <ap*******@localhost.localdomain> wrote:
On 2006-05-30, Keith Thompson <ks***@mib.org> wrote:
Andrew Poelstra <ap*******@localhost.localdomain> writes:
On 2006-05-30, Eric Sosman <Er*********@sun.com> wrote: [...]
Why does it tell me that -42 is not divisible by 3? Because it is adding ~3, and in general bit twiddling is undefined
when working with negative integers. I should have caught that. int n; should be unsigned int n;, or a better solution would have
been to set n to abs(n) right before the while loop.

Any other special cases I should know about?


I haven't been able to convince myself that it works
in one's complement.
--
Prototypes are supertypes of their clones. -- maplesoft
May 30 '06 #15
On 2006-05-30, Walter Roberson <ro******@ibd.nrc-cnrc.gc.ca> wrote:
In article <sl**********************@localhost.localdomain> ,
Andrew Poelstra <ap*******@localhost.localdomain> wrote:
On 2006-05-30, Keith Thompson <ks***@mib.org> wrote:
Andrew Poelstra <ap*******@localhost.localdomain> writes:
On 2006-05-30, Eric Sosman <Er*********@sun.com> wrote:
[...]
> Why does it tell me that -42 is not divisible by 3? Because it is adding ~3, and in general bit twiddling is undefined
when working with negative integers. I should have caught that. int n; should be unsigned int n;, or a better solution would have
been to set n to abs(n) right before the while loop.

Any other special cases I should know about?


I haven't been able to convince myself that it works
in one's complement.


Because I'm taking negative numbers, adding 3, and then taking
the absolute value, no bit twiddling is done until the number is
guaranteed to be positive.

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
You can lead a blind man to water but you can't make him chug it.
May 31 '06 #16
On 2006-05-30, Eric Sosman <Er*********@sun.com> wrote:


Andrew Poelstra wrote On 05/30/06 17:36,:
On 2006-05-30, Keith Thompson <ks***@mib.org> wrote:
Andrew Poelstra <ap*******@localhost.localdomain> writes:

On 2006-05-30, Eric Sosman <Er*********@sun.com> wrote:

[...]

> Why does it tell me that -42 is not divisible by 3?
>

Because it is adding ~3, and in general bit twiddling is undefined
when working with negative integers. I should have caught that.

int n; should be unsigned int n;, or a better solution would have
been to set n to abs(n) right before the while loop.

And if n == INT_MIN?

You would need to check for that, and then add three to ensure that you
don't overflow when you abs() it.

Any other special cases I should know about?


Only two that I can think of off-hand, both having
to do with issues of representation. Hint #1: Is the
absolute value of ~3 greater or less than a thousand?
Hint #2: Is the value of ~3 even or odd? (See section
6.2.6.2, paragraph 2).

I don't have a Standard beside me right now; could you quote
6.2.6.2 for me?

I can't think of a reason for either hints to affect me. Here
is my current code (OP, I'd like some credit for your course):

#include <stdio.h>
#include <math.h>

int main(void)
{
signed int n; /* Best to be explicit about signedness */
unsigned int o; /* in this code. */

printf ("Enter a number: ");
scanf ("%d", &n);

if (n < 0)
{
n += 3; /* Ensure that n isn't INT_MIN */
o = abs(n);
} else {
o = n;
}

/* From now on, o is our variable, and is unsigned. */
/* Note that 0 <= o <= INT_MAX */

while (o >= 3)
o += (unsigned int) (~3) + 1; /* Is this cast necessary? */

if (o == 0)
printf ("\nThat number is divisible by 3\n");
else
printf ("\nThat number is not divisible By 3\n");

return 0;
}
For now I'm going to flip through my Discrete Mathematics
textbook and see if I can come up with a cleverer algorithm
for determining 3-divisibility in base 2.
The principal lesson is that it is usually silly to
use "bit fiddling" for tasks of the kind posed by the
O.P. (more likely, by the O.P.'s homework assignment).
As this thread demonstrates, such tasks can be trickier
than they might appear, and the "obvious" solutions have
pitfalls. Avoiding the pits can consume more execution
time than you save by avoiding the % or the * or whatever.

Let's try to imagine a scenario where speeding up a
divisibility-by-three test would make a useful improvement.
On the six-year-old machine sitting in front of me at the
moment, it takes about 0.172 seconds to test a million
numbers for divisibility by three using the % operator.
By resorting to bit fiddling I can cut the time to just
0.013 seconds, better than a thirteenfold speedup. Wow!


Let's consider that right now I'm running slrn on a 12-year-old
machine, and every so often I boot up a computer that is now about
15 years old to work on. Micro-optimization makes a big difference
on those computers.

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
You can lead a blind man to water but you can't make him chug it.
May 31 '06 #17

saurabhnsit2001 wrote:
I have got few questions on bit fiddling!!
1. Fast way of Multiplying a No by 7.
2. Counting no of set bits in a 32 bit no without looping.
3. finding id a no is divisible by 3 or not without using %, /, and -
operators,


Ans for Q1 is

n*7= n*(8-1)=n*8-n=n<<3-n

Ans for Q2 is

#include<stdio.h>
int main()
{
int n,c;
scanf("%d",&n);
c=0;
while(n!=0)
{
n&=n-1;
c++;
}
printf("\n No of One's in %d=%d",n,c);
}

May 31 '06 #18
ja********@gmail.com wrote:
saurabhnsit2001 wrote:
I have got few questions on bit fiddling!!
1. Fast way of Multiplying a No by 7.
2. Counting no of set bits in a 32 bit no without looping.
3. finding id a no is divisible by 3 or not without using %, /, and -
operators,
Ans for Q1 is

n*7= n*(8-1)=n*8-n=n<<3-n


What if n*8 overflows, but n*7 doesn't?
Ans for Q2 is
[...]
while(n!=0)


I don't think "without looping" is a typo for "with looping".

May 31 '06 #19
ja********@gmail.com schrieb, Am 31.05.2006 07:29:
Ans for Q1 is
n*7= n*(8-1)=n*8-n=n<<3-n


n<<3-n
is equivalent to
n<<(3-n)
May 31 '06 #20


Andrew Poelstra wrote On 05/31/06 00:46,:

I can't think of a reason for either hints to affect me. Here
is my current code (OP, I'd like some credit for your course):

#include <stdio.h>
#include <math.h>

int main(void)
{
signed int n; /* Best to be explicit about signedness */
unsigned int o; /* in this code. */

printf ("Enter a number: ");
scanf ("%d", &n);

if (n < 0)
{
n += 3; /* Ensure that n isn't INT_MIN */
o = abs(n);
} else {
o = n;
}

/* From now on, o is our variable, and is unsigned. */
/* Note that 0 <= o <= INT_MAX */

while (o >= 3)
o += (unsigned int) (~3) + 1; /* Is this cast necessary? */
It's not necessary, but it's also not sufficient.
The value of `(unsigned int) (~3) + 1' depends on the
way the implementation represents signed integers:

- Two's complement: ~3 is equal to -4, the cast
converts it to UINT_MAX+1-4 == UINT_MAX-3, and
adding 1 gives UINT_MAX-2.

- Ones' complement: ~3 is equal to -3, the cast
converts it to UINT_MAX+1-3 == UINT_MAX-2, and
adding 1 gives UINT_MAX-1.

- Signed magnitude: ~3 is equal to -(INT_MAX-3),
the cast converts it to UINT_MAX+1+INT_MAX-3
== INT_MAX-3 (probably), and adding 1 gives
INT_MAX-2.

You should have written `~(unsigned int)3 + 1',
or simply `~3u + 1' (or even `~3u + 1u').
if (o == 0)
printf ("\nThat number is divisible by 3\n");
else
printf ("\nThat number is not divisible By 3\n");

return 0;
}
For now I'm going to flip through my Discrete Mathematics
textbook and see if I can come up with a cleverer algorithm
for determining 3-divisibility in base 2.

The principal lesson is that it is usually silly to
use "bit fiddling" for tasks of the kind posed by the
O.P. (more likely, by the O.P.'s homework assignment).
As this thread demonstrates, such tasks can be trickier
than they might appear, and the "obvious" solutions have
pitfalls. Avoiding the pits can consume more execution
time than you save by avoiding the % or the * or whatever.

Let's try to imagine a scenario where speeding up a
divisibility-by-three test would make a useful improvement.
On the six-year-old machine sitting in front of me at the
moment, it takes about 0.172 seconds to test a million
numbers for divisibility by three using the % operator.
By resorting to bit fiddling I can cut the time to just
0.013 seconds, better than a thirteenfold speedup. Wow!

Let's consider that right now I'm running slrn on a 12-year-old
machine, and every so often I boot up a computer that is now about
15 years old to work on. Micro-optimization makes a big difference
on those computers.


On the machine mentioned above, mod-and-zero-test took
0.172 seconds to test the numbers 0..999999 for divisibility
by three. Division by repeated subtraction took 474 seconds
to do the same thing. Slowing something down by more than
two hundred seventy-five thousand percent is certainly "a big
difference," but few people would call it an "optimization."

--
Er*********@sun.com

May 31 '06 #21
On Wed, 31 May 2006 04:46:29 GMT, Andrew Poelstra
<ap*******@localhost.localdomain> wrote:
On 2006-05-30, Eric Sosman <Er*********@sun.com> wrote:

Andrew Poelstra wrote On 05/30/06 17:36,:
On 2006-05-30, Keith Thompson <ks***@mib.org> wrote:

Andrew Poelstra <ap*******@localhost.localdomain> writes: Any other special cases I should know about?
Only two that I can think of off-hand, both having
to do with issues of representation. Hint #1: Is the
absolute value of ~3 greater or less than a thousand?
Hint #2: Is the value of ~3 even or odd? (See section
6.2.6.2, paragraph 2).

I don't have a Standard beside me right now; could you quote
6.2.6.2 for me?

I'm not going to bother posting all of it with my delay, but I have a
useful if embarassing mnemonic: 6.2.6 is about representation (and .2
about integers) because it immediately follows 6.2.5 types which used
to be 6.1.2.5, so treated as a pseudo obsolete stock quotation it
incremented from 6 1/8 to 6 1/4 . Yes, I do need to get a life.
I can't think of a reason for either hints to affect me. Here
is my current code (OP, I'd like some credit for your course):
That section (now explicitly) allows ones' complement and sign and
magnitude but your bitwise-complement plus one trick only works in
two's complement.
#include <stdio.h>
#include <math.h>

int main(void)
{
signed int n; /* Best to be explicit about signedness */
unsigned int o; /* in this code. */

printf ("Enter a number: ");
scanf ("%d", &n);
Should check for scan error, or at least have initialized the auto n
to a non-indeterminate value in case of that.
if (n < 0)
{
n += 3; /* Ensure that n isn't INT_MIN */
o = abs(n);
} else {
o = n;
}

/* From now on, o is our variable, and is unsigned. */
/* Note that 0 <= o <= INT_MAX */

while (o >= 3)
o += (unsigned int) (~3) + 1; /* Is this cast necessary? */
It's not even sufficient. In 1sC ~3 is -3, which becomes UINT_MAX+1
-3; +1 gives UINT_MAX+1 -2, which has the effect in unsigned=modular
arithmetic of subtracting two. Without the cast ~3 + 1 becomes -2,
which is converted to unsigned and effectively subtracts 2. In S&M ~3
is a huge negative number (-INT_MAX+3), and I'm pretty sure there's no
case where it works regardless of how unsigned uses the sign bit
and/or any padding bits.

You need to have unsigned _before_ complementing:
~(unsigned)3 + 1 or just ~3U + 1
The principal lesson is that it is usually silly to
use "bit fiddling" for tasks of the kind posed by the
O.P. (more likely, by the O.P.'s homework assignment).

Let's consider that right now I'm running slrn on a 12-year-old
machine, and every so often I boot up a computer that is now about
15 years old to work on. Micro-optimization makes a big difference
on those computers.


Even on those computers subtraction does tend to be available.
(Although on PDP-8, and I assume -5, it is generally 1 insn more than
addition -- which is indeed 2sC.) But iterated subtraction or addition
is unlikely to be an optimization for any but quite small values --
which could probably as well or better be optimized as a lookup table
on even machines that are 30 to 40 years old. (If you can afford the
power and airconditioning to run one. Or more likely emulate it.)

- David.Thompson1 at worldnet.att.net
Jun 8 '06 #22

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

Similar topics

1
by: saurabhnsit2001 | last post by:
I have got few questions on bit fiddling!! 1. Fast way of Multiplying a No by 7. 2. Counting no of set bits in a 32 bit no without looping. 3. finding id a no is divisible by 3 or not without...
22
by: dos.fishing | last post by:
Hello, I'm writing a function that should do the following: /** * Calculate and return fraction of valueA where max fractions is 31. * param valueA A five bit value, 0-31. * param valueB The...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
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,...
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,...

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.