Dividing int numbers by 256, but fast!

This is a discussion on Dividing int numbers by 256, but fast! within the C++ Programming forums, part of the General Programming Boards category; OK, I have a lot of integers, and I want to sorta simulate floating point by treating all numbers as ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    125

    Dividing int numbers by 256, but fast!

    OK, I have a lot of integers, and I want to sorta simulate floating point by treating all numbers as if they're several times smaller than they really are. However, in the end, I'll still have to divide by the number I used. Now, I heard dividing is relatively slow, so I thought it should be possible to speed it up by dividing by 256, by reading only the first 3 bytes of the int. How should I go about this?

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    You could look at bitshifting.......this ises the '<<' and '>>' operators

    The first chapter of Randal Hydes Art of Assembly is called "Data Representation" and covers bit manipulation real well - all be it in an assembler book....but no ASM is in this chapter (Well its the first chapter on the X86 version....probably a little later in the newer versions)

    Try here

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    125
    Hmm... That way, I'd have to shift all bits 8 positions to the right.
    What's the syntax of those operators?

    ( EDIT: ) OK, I looked it up. Do these things work for ints like they do for um... other things? Would N >>= 8 do the trick?
    Last edited by Boksha; 04-04-2002 at 02:43 PM.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    X>>=8;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    125
    OK, that was exactly what I needed. Thank you both!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  4. Switch/case Problems (long code in post)
    By Wraithan in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2005, 05:40 PM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21