clicky75
clicky75

Reputation: 11

Separating a 32 bit address into parts

In C, If I have a 32 bit binary address, how could I take bits 6 - 10 (5 bit value) and assign them to a new variable? For example, the address: 00000001001010011000100100100011

I want to take this section:

0000000100101001100010 01001 00011

And store it in a variable, x.

Upvotes: 1

Views: 664

Answers (1)

chux
chux

Reputation: 154272

If I have a 32 bit binary address, how could I take bits 6 - 10 (5 bit value)

Addresses are not really something that have arithmetic values in particular bits - that is very implementation dependent. Code can convert an address to an integer type and then proceed.

OP apparently wants other @paddy bits: 9 to 5.

#include <stdint.h>

void *ptr = ....;
uintptr_t i = (uintptr_t) ptr;

//           v------v         Ignore 5 least significant bits, move all bits right by 5. 
unsigned x = (i >> 5) & 0x1F;
//                      ^--^  Mask: Only retain 5 bits.

Upvotes: 3

Related Questions