Thread: Anyone good with bitwise operations/page detection

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    51

    Anyone good with bitwise operations/page detection

    I am having difficulty determining whether or not two addresses are on the same page. First let me define a page.

    A page is an area in memory that is 255 bytes. The first page (or called zero page) is in this range:
    0x0000-0x00FF

    The second page would be in this range:
    0x0100-0x01FF

    and so on...

    NOTE: addresses will range from 0x0000-0xFFFF

    I need to write a quick algorithm to determine whether two addresses, lets say 0x0148 and 0x0192 are in the same page (which in this example they are).

    I could do something easy like comparing the upper and lower bounds of both addresses to a page to determine if they are all equal to 1 (hence four operations, two for each address.. four if statements but way too much code) but I need something very fast because this piece of code will be executed the most. Any one have any ideas on this? Thanks.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps this:
    Code:
    int isSamePage(int addr1, int addr2)
    {
        return ((addr1 & 0xFF00) == (addr2 & 0xFF00));
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    51
    Quote Originally Posted by matsp View Post
    Perhaps this:
    Code:
    int isSamePage(int addr1, int addr2)
    {
        return ((addr1 & 0xFF00) == (addr2 & 0xFF00));
    }
    --
    Mats
    I'll try that, thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ray tracer and collision detection
    By hdragon in forum Game Programming
    Replies: 31
    Last Post: 01-19-2006, 11:09 AM
  2. Tough Bitwise Exercise
    By Charmy in forum C++ Programming
    Replies: 9
    Last Post: 09-08-2005, 06:27 PM
  3. Good resources for maths and electronics
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 12-22-2004, 04:23 PM
  4. Good C++ books for a begginer
    By Rare177 in forum C++ Programming
    Replies: 13
    Last Post: 06-22-2004, 04:30 PM
  5. good collision detection routines
    By cozman in forum Game Programming
    Replies: 1
    Last Post: 10-19-2001, 01:05 PM