Results 1 to 17 of 17

Thread: More of a math question

  1. #1

    Thread Starter
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024

    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


  2. #2
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    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.

  3. #3

    Thread Starter
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    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


  4. #4
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    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.

  5. #5

    Thread Starter
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    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


  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  7. #7
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    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/

  8. #8
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    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.

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  10. #10

    Thread Starter
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    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


  11. #11
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  12. #12
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  13. #13

    Thread Starter
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    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


  14. #14
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  15. #15

    Thread Starter
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    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


  16. #16
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    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.

  17. #17
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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
  •  



Click Here to Expand Forum to Full Width