Bits and Addresses

This is a discussion on Bits and Addresses within the C++ Programming forums, part of the General Programming Boards category; I'm writing a memory management program/simulation. Let's say I have a 32-bit pointer. I want it to represent a virtual ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    186

    Bits and Addresses

    I'm writing a memory management program/simulation. Let's say I have a 32-bit pointer. I want it to represent a virtual address. Part of that address, let's say the upper 22 is the virtual page number, and the lower 10 is the page offset. How do I do bit selection in C++? How do I say something like:
    int upper = upper22bits(pointer);
    int lower = lower 10bits(pointer);

    Thanks

    I seem to remember messing around with shifts and masking but I don't really remember how.
    For example if I want the upper 22 its, I think I'd right shift 10, & with enough 1's to cover the bits (2^22-1 I think) and I'd have a number represented by the upper 22 bits.
    If I want the lower 10 bits I think I don't have to shift, I just & with enough 1's to cover the bits (2^10-1 I think).
    I'm not sure if this is correct

    I tested it out and it prints out the wrong stuff
    Code:
    cout << "Testing bit selection" << endl;
    int test = 23; // 23 = "10111"
    cout << "Lower three bits: " << (test & 2^3-1) << endl; // "111" = 7
    cout << "Upper three bits: " << (test>>2 & 2^3-1) << endl; //"101" = 5
    Last edited by jcafaro10; 03-15-2009 at 02:15 PM.

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Code:
    2^3-1
    2^3-1 is 0. However, the & will happen first (it has higher precedence that ^, see your Order of Operations)
    The bigger issue, however, I suspect that ^ is not what you're expecting it to be. ^ is binary XOR, not exponent. (There is no C operator for exponent.)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,456
    (There is no C operator for exponent.)
    actually when you need power of 2 you can use C operator - shift

    1<<3
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    Thanks, that did it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I extract the most significant 4 bits?
    By mr_coffee in forum C Programming
    Replies: 6
    Last Post: 04-03-2009, 01:40 PM
  2. how a program malloc more then 4Gb ?
    By jabka in forum C Programming
    Replies: 4
    Last Post: 10-06-2007, 05:00 PM
  3. IPv6 multicast example code
    By Sang-drax in forum Networking/Device Communication
    Replies: 7
    Last Post: 07-25-2005, 09:26 AM
  4. question about 32 bit addresses??
    By newbie02 in forum C++ Programming
    Replies: 3
    Last Post: 09-12-2003, 02:02 PM
  5. Bits & Bytes
    By jon_nc17 in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-13-2002, 02:56 PM

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