|
-
Nov 4th, 2003, 04:15 PM
#1
Thread Starter
Frenzied Member
More of a math question
I have a var called i that is a counter in a loop. I need to know everytime it hits a number that is divisable by 32 without reseting the counter. So I need to know when it hits 32, 64, 96, 128, 160, etc. This can go up to 8,000 or so. Anyone got some spiffy formula that can help me out?
MSVS 6, .NET & .NET 2003 Pro
I HATE MSDN with .NET & .NET 2003!!!
Check out my sites:
http://www.filthyhands.com
http://www.techno-coding.com

-
Nov 4th, 2003, 04:43 PM
#2
Code:
for (int i = 0; i < 5795279; ++i)
{
if (i % 32 == 0)
{
// i is divisible by 32.
}
}
The modulo operator (a % b) returns the remainder after a is divided by b. so when there is no remainder, i is divisible by 32.
I'm sure you could also do it by looking at the bits, which would be faster, but I can't think how right now.
Last edited by sunburnt; Nov 4th, 2003 at 04:48 PM.
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Nov 4th, 2003, 04:44 PM
#3
Thread Starter
Frenzied Member
I had thought of that, but I really didnt want to waste cycles looping. I was thinking there must be a better way.
MSVS 6, .NET & .NET 2003 Pro
I HATE MSDN with .NET & .NET 2003!!!
Check out my sites:
http://www.filthyhands.com
http://www.techno-coding.com

-
Nov 4th, 2003, 04:49 PM
#4
Am I missing something? Just put this inside your loop:
Code:
if (counter % 32 == 0)
{
// counter is divisible by 32.
}
it won't modify the counter variable.
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Nov 4th, 2003, 04:51 PM
#5
Thread Starter
Frenzied Member
Sorry, my mistake I misread what you wrote. Thanks
MSVS 6, .NET & .NET 2003 Pro
I HATE MSDN with .NET & .NET 2003!!!
Check out my sites:
http://www.filthyhands.com
http://www.techno-coding.com

-
Nov 5th, 2003, 05:36 AM
#6
Since 32 is a power of two, it can be made far faster by writing
if((i & 0x1F) == 0)
though a good compiler should do that on its own. The % is more readable.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 5th, 2003, 01:09 PM
#7
yay gay
Originally posted by CornedBee
Since 32 is a power of two, it can be made far faster by writing
if((i & 0x1F) == 0)
though a good compiler should do that on its own. The % is more readable.
could you explain what does the (i & 0x1F) == 0) really do please?
i can see 1F = 31 but what more?
\m/  \m/
-
Nov 5th, 2003, 02:40 PM
#8
CornedBee is talking about what I mentioned in my first post, checking the bits of the number, which is probably far faster.
0x1F = 31 = (MSB first) 0000 0000 0001 1111
if (number & 0x1F) will only return != 0 if number has one of the five lowest bits set. Since all multiples of 32 will never have these bits set, it's a good test.
IE:
0000 0000 0010 0000 [32] &
0000 0000 0001 1111 [31]
===================
0000 0000 0000 0000 [00]
0000 0000 0010 0100 [36] &
0000 0000 0001 1111 [31]
===================
0000 0000 0000 0100 [04]
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Nov 5th, 2003, 03:16 PM
#9
transcendental analytic
the & operator is called bitwise and, and performs the boolean and operation on each bit of its operands and result corresponding bit, all encoded as digits in the binary integers.
for efficiency use nested loops for this sort of things, let the inner loop go 32 cycles, or less as by avoided ifs in loops, better pipelining...
Last edited by kedaman; Nov 5th, 2003 at 03:19 PM.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Nov 5th, 2003, 03:31 PM
#10
Thread Starter
Frenzied Member
Using bitwise is a bit faster. I can tell by how much, but it cut a couple of seconds off.
MSVS 6, .NET & .NET 2003 Pro
I HATE MSDN with .NET & .NET 2003!!!
Check out my sites:
http://www.filthyhands.com
http://www.techno-coding.com

-
Nov 5th, 2003, 04:13 PM
#11
transcendental analytic
are you sure you're incrementing the same variable? nested loops should be faster, unless you do 32 times more work without the ifs
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Nov 5th, 2003, 04:19 PM
#12
transcendental analytic
ok here's some sample code
Code:
x=8000;
while (x-=32){
y=32;
while (y--){
}
//here goes code you'd have in the ifs
}
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Nov 5th, 2003, 04:21 PM
#13
Thread Starter
Frenzied Member
I dont think nested would be better in this case. I am looping through a rather large vector of a ton of data, using an iterator, which is being dumped to an excel spread sheet. I need to know when I hit 32 because thats where the break in the data needs to happen on each page, thats all. So I think nesting would just make it more complex.
MSVS 6, .NET & .NET 2003 Pro
I HATE MSDN with .NET & .NET 2003!!!
Check out my sites:
http://www.filthyhands.com
http://www.techno-coding.com

-
Nov 5th, 2003, 04:23 PM
#14
transcendental analytic
well in case you want to keep it readable, then do so. it should be though, less efficient
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Nov 5th, 2003, 04:26 PM
#15
Thread Starter
Frenzied Member
Actually that doesnt take really that much time. I need to shave more time off the creating of the final vector. Thats what takes the most time. But thats going to have to wait for another time, I got a dead line.
MSVS 6, .NET & .NET 2003 Pro
I HATE MSDN with .NET & .NET 2003!!!
Check out my sites:
http://www.filthyhands.com
http://www.techno-coding.com

-
Nov 5th, 2003, 04:52 PM
#16
i%32 should not be any slower than 1&31, any decent compiler will optimize the first statement to the last. You shouldn't waste your time optimizing details like this.
-
Nov 5th, 2003, 05:31 PM
#17
transcendental analytic
its the conditionals that slow you down, dunno if compilers break them down though, you could check the output asm code
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|