How can I extract the most significant 4 bits?

This is a discussion on How can I extract the most significant 4 bits? within the C Programming forums, part of the General Programming Boards category; Hi guys, I'm currenty writing a program to simulate page replacement, and I'm given a series of virtual addresses in ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    47

    Talking How can I extract the most significant 4 bits?

    Hi guys, I'm currenty writing a program to simulate page replacement, and I'm given a series of virtual addresses in the form of:

    0x6873
    0x6cff
    0x68ec
    0xb148

    I'm currently going to check to see if the v-address is in the TLB (translation look aside buffer) and if its not in there I'm going to check to see if its in the Page table.

    But what I need to do is extract the most signifincant 4 bits and convert that to an integer and use that number as the page # to look inside the tlb/page table.

    So if i was given 0xb148, I need to extract the b.

    Any ideas?

    Thanks

    NOTE: I'm reading and storing those addresses in an unsigned integer: if ( fscanf( fp, "%d %x\n", pid, vaddr ) == 2 ); so vaddr contains the value 0xb148, and vaddr data type is: unsigned int *vaddr
    Here's the full code of me reading the file and assigning the vaddress to the varaible:
    Code:
    int get_memory_access( FILE *fp, int *pid, unsigned int *vaddr, int *op, int *eof )
    {
      int err = 0;
      *op = 0;   /* read */
    
      /* create processes, including initial page table */
      if ( fscanf( fp, "%d %x\n", pid, vaddr ) == 2 );
      else *eof = 1;
    
      /* write: for certain addresses (< 0x200) */
      if (( *vaddr - (( *vaddr / PAGE_SIZE ) * PAGE_SIZE)) < 0x200 ) {
        *op = 1; 
        printf( "get_memory_access: process %d writes at 0x%x\n", *pid, *vaddr );
      }
    
      return err;
    }


    Someone told me to do the following:
    vaddress >> 12
    vaddress meaning that is what is storing the value: 0xb148
    but i'm not sure how this is working exactly
    Last edited by mr_coffee; 04-03-2009 at 01:07 PM.

  2. #2
    Registered User draugr's Avatar
    Join Date
    Mar 2009
    Posts
    9
    This is easy with the bitwise operator &

    If you have
    int x = 0xb148;
    you can extract the 'b' with a mask 0xf000
    so you'd do:
    int res = x & 0xf000;
    and now res == 0xb000

    Another example: if you wanted to extract 0xb008 from 0xb438, you would use 0xf00f as the mask.

  3. #3
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    int extracted_nibble;
    extracted_nibble=0xb148 & 0xf000;

    you'll get b000 in "extracted_nibble" variable

    then you can right shift or leave it like that.
    Last edited by creeping death; 04-03-2009 at 01:08 PM. Reason: not left shift right shift , i suck at directions
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    47
    Awesome, thanks guys!

    so say I wanted to extract 148 from 0xb148, could I simply do:
    int result = 0xb148 & 0x0fff

    then would result contain: 0x0148?

  5. #5
    Registered User draugr's Avatar
    Join Date
    Mar 2009
    Posts
    9
    Exactly.
    This is easy to imagine in binary, rather than hexadecimal.

    Code:
    res = num & mask;
    num  = 01101101  (0x6d)
    mask = 11110000  (0xf0)
    ----------------
    res  = 01100000  (0x60)
    See? Everywhere the mask has 0, the result will be 0, if the mask is 1, the result will be the original bit.
    You can imagine the mask is overlayed over the number, 1s are transparent, 0s are opaque.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    47
    ahh, excellent! thanks guys, makes sense now.

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Quote Originally Posted by draugr View Post
    Exactly.
    This is easy to imagine in binary, rather than hexadecimal.
    If you do enough of this it becomes easy to imagine it in hex as well.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SDLKey to ASCII without unicode support?
    By zacs7 in forum Game Programming
    Replies: 6
    Last Post: 10-07-2007, 03:03 AM
  2. Help counting number of bits set in an integer
    By JayDiddums10 in forum C Programming
    Replies: 5
    Last Post: 12-07-2006, 02:21 PM
  3. Writing binary data to a file (bits).
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 03:53 PM
  4. New idea on conveting byte to bits/bits to byte
    By megablue in forum C Programming
    Replies: 10
    Last Post: 10-26-2003, 01:16 AM
  5. Help Please...bits
    By Unregistered in forum C Programming
    Replies: 11
    Last Post: 01-24-2002, 12:43 PM

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