Bitwise questions with the following code

K

Krumble Bunk

Good morning c.l.c,

I have a question regarding the following snippet of code.

static inline void WriteMemory(register word Address,register byte
Value)
{
MemoryPage[Address>>13][Address&0x1FFF]=Value;
}


1) Why is 'Address' shifted right by 13 places? What is that actually
doing? (apart from dividing). What i'm getting at, is why use those
precise values? 13 and 0x1FFF - what's special about them, indivudually
and in combination in the 2D array?

I realise there's not an awful lot of information to go on, but any
help is very much appreciated.

Thanks

KB
 
W

Walter Roberson

Krumble Bunk said:
I have a question regarding the following snippet of code.
static inline void WriteMemory(register word Address,register byte
Value)
{
MemoryPage[Address>>13][Address&0x1FFF]=Value;
}

1) Why is 'Address' shifted right by 13 places?

Why not?
What is that actually
doing? (apart from dividing).

Bit sub-selection.
What i'm getting at, is why use those
precise values? 13 and 0x1FFF - what's special about them, indivudually
and in combination in the 2D array?

0x1FFF is the mask that selects exactly 13 trailing bits.
(2<<N)-1 is a binary mask used to select N trailing bits.

Why 13 instead of (say) 12 or 14? Insufficient information.
Presumably the above is either implimenting or mimicing a real
hardware situation.
 
C

CBFalconer

Krumble said:
I have a question regarding the following snippet of code.

static inline void WriteMemory(register word Address,register byte
Value)
{
MemoryPage[Address>>13][Address&0x1FFF]=Value;
}

1) Why is 'Address' shifted right by 13 places? What is that
actually doing? (apart from dividing). What i'm getting at, is
why use those precise values? 13 and 0x1FFF - what's special
about them, indivudually and in combination in the 2D array?

Apart from the fact that the types word and byte have not been
defined, you might think about the number of bits preserved by the
mask 0x1fff. Then consider the physical organization of your
system (which is OT here).

In general you would be better advised to include spaces in your
expressions for clarity. The blank shortage ended a few years ago.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 
K

Krumble Bunk

K

Krumble Bunk

A

Arndt Jonasson

Krumble Bunk said:
Good morning c.l.c,

I have a question regarding the following snippet of code.

static inline void WriteMemory(register word Address,register byte
Value)
{
MemoryPage[Address>>13][Address&0x1FFF]=Value;
}


1) Why is 'Address' shifted right by 13 places? What is that actually
doing? (apart from dividing). What i'm getting at, is why use those
precise values? 13 and 0x1FFF - what's special about them, indivudually
and in combination in the 2D array?

If the programmer had followed the practice of giving names to the
"magic numbers" in the code, it would have been easier for you to
obtain the answers to those questions. See for example:

http://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_code
 
M

Martin Ambuhl

Krumble said:
Good morning c.l.c,

I have a question regarding the following snippet of code.

static inline void WriteMemory(register word Address,register byte
Value)
{
MemoryPage[Address>>13][Address&0x1FFF]=Value;
}


1) Why is 'Address' shifted right by 13 places? What is that actually
doing? (apart from dividing). What i'm getting at, is why use those
precise values? 13 and 0x1FFF - what's special about them, indivudually
and in combination in the 2D array?

I realise there's not an awful lot of information to go on, but any
help is very much appreciated.

This is really not a C question, but about a particular platform's
addressing scheme. That makes CLC the wrong place for the question.
However, it appears that the memory mapping is done by page+offset; If
'Address' is 16 bits (the type 'word' is not a C type), then the page is
in bits 13-15 and the offset in bits 0-12. Address & 01FF yields the
offset; Address >> 13 yields the page.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,174
Messages
2,570,940
Members
47,484
Latest member
JackRichard

Latest Threads

Top