1

I am currently working on a program to simulate a cache in C. I have been researching, I know how to set and test individual bits, but how could I extract x amount of bits from within the middle of a 64 bit hex address?

For example, I have a 64 bit hex address. The first 2 bits are dedicated for the block offset, the next 2 bits are dedicated for the set index, and the last 60 for the tag. How could I extract the first 2 bits into an unsigned integer? How could I extract the next 2 bits?

EDIT: This is just an example. I am hoping for a solution that will let you get any amount of bits anywhere in the address

SECOND EDIT: When I say first 2 bits, I mean the least significant bits. My apologies.

Jakeway
  • 241
  • 1
  • 2
  • 10
  • 1
    The phrase "first" in "first 2 bits" is open to interpretation: do you mean the first 2 least significant bits or the first two most significant bits? Better to use "least significant" or "most significant" rather than "first". The 2 answers reflect that confusion. – chux - Reinstate Monica Dec 15 '13 at 08:49

2 Answers2

2

Cast to uintptr_t from <stdint.h> and use >> and & to extract bits.

E.g.

uintptr_t ptr_int = (uintptr_t) ptr;
unsigned int bits1 = (unsigned int)((ptr_int >> 62) & 0x3)  // First two most significant bits
unsigned int bits2 = (unsigned int)((ptr_int >> 60) & 0x3)  // Next two most significant bits
Community
  • 1
  • 1
ugoren
  • 16,023
  • 3
  • 35
  • 65
1
typedef struct parse64 {
    unsigned int get:2,
    unsigned int set:2,
    unsigned int tag:60 // if your system is a64 bit system
} parse64;

In your code:

struct parse64 *parser;
parser = (struct parse64 *) data;

unsigned int get = parser->get;

based on your edit

EDIT: This is just an example. I am hoping for a solution that will let you get any amount of bits anywhere in the address

you can get your data with bitwise operation too:

get = data & 0x03 //in binary 0X03 is equivalent to 00....000011
set = (data & 0x12)>>2 //in binary 0X12 is equivalent to 00....001100
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
  • 3
    That might be the correct layout for the bit-fields; it is not guaranteed to be the correct layout. Actually, very little is guaranteed about bit fields by the standard. – Jonathan Leffler Dec 15 '13 at 07:40
  • @JonathanLeffler could you clarify more or give a link for this ( SO topic) – MOHAMED Dec 15 '13 at 07:43
  • That would be a good solution, however I need to generalize it for any amount of bits dedicated to tag, set index, etc – Jakeway Dec 15 '13 at 07:43
  • @Tom you cab use bitwise operation. see my answer for details – MOHAMED Dec 15 '13 at 07:51
  • Amongst others: [SO 10128044](http://stackoverflow.com/questions/10128044/), [SO 18284640](http://stackoverflow.com/questions/18284640/), [SO 13105313](http://stackoverflow.com/questions/13105313/), [SO 14336994](http://stackoverflow.com/questions/14336994/), [SO 1490092](http://stackoverflow.com/questions/1490092/). – Jonathan Leffler Dec 15 '13 at 07:53