Bitwise Operations in Physical Memory Manager

If you are new to OS Development, plan on spending some time here first before going into the other forums.

Moderator:Moderators

Post Reply
AndyB
Posts:3
Joined:Thu Feb 10, 2011 11:27 am
Bitwise Operations in Physical Memory Manager

Post by AndyB » Thu Feb 10, 2011 11:55 am

Hi, So I have been reading the articles over the last couple of weeks along with the osdev.org wiki and several other osdev resources. My main interest is to expand my knowledge in how lower level systems work.

While I am familiar with bitwise operations, its been about a year since ive done any work with them and im trying to break apart the operations for the set/unset functions for the memory map.

For example: The "void mmap_set (int bit)":

Code: Select all

inline void mmap_set (int bit)
{
  _mmngr_memory_map[bit / 32] |= (1 << (bit % 32));
}
So, lets take I want to set the 23rd bit. and lets also say that...

Code: Select all

map[0] = 1111 1111 0011 1100 0000 0000 0000 0000 0000
If I break this down with my understanding:

bit = 23
i = 0 (map array index (23 / 32) )

map[0] |= (1 << (bit % 32 ) )
map[0] |= (1 << (23) )

Now, This is where I get confused.
(1 << 23) = 0000 0000 1000 0000 0000 0000 0000 0000 0000

but if you OR that with map[0]

Code: Select all

0000 0000 0100 0000 0000 0000 0000 0000
1111 1111 0011 1100 0000 0000 0000 0000 
You are effectively setting the 10th bit not the 23rd bit?

Could someone explain where I am misunderstanding this please?

By my understanding it should be a right shift (1 >> 23) to get

Code: Select all

(1 >> 23) = 0000 0000 0000 0000 0000 0010 0000 0000
That would give you a mask for the 23rd bit?

Thanks for your time in reading.
Andy

halofreak1990
Posts:92
Joined:Thu May 27, 2010 8:54 pm
Location:Netherlands

Re: Bitwise Operations in Physical Memory Manager

Post by halofreak1990 » Thu Feb 10, 2011 1:03 pm

Bits are read from right to left like this:

Code: Select all

bit 31                                       bit 0
      0000 0000 1000 0000 0000 0000 0000 0000
which would make the set bit bit 23

AndyB
Posts:3
Joined:Thu Feb 10, 2011 11:27 am

Re: Bitwise Operations in Physical Memory Manager

Post by AndyB » Thu Feb 10, 2011 1:17 pm

Yes but memory is linear from left to right.

Surely Bit 0 - Bit 1 - Bit 2 - Bit 3 should represent Block 1 - Block 2 - Block 3 - Block 4.

So therefore to "reserve" the 23rd block you would set the 23rd bit left to right?

User avatar
Mike
Site Admin
Posts:465
Joined:Sat Oct 20, 2007 7:58 pm
Contact:

Re: Bitwise Operations in Physical Memory Manager

Post by Mike » Thu Feb 10, 2011 7:19 pm

Hello,

"memory is linear from left to right" isnt entirely accurate. In little endian systems, a dword value like 0x0a0b0c0d is stored as 0d 0c 0b 0a in memory. Notice how the dword is completely backward when stored in memory.

This fact can be verified using C byte pointers to access an integer:

Code: Select all

int data=0x0a0b0c0d;
char* p = (char*) &data;
Now p[0]=0xd, p[1]=0xc, p[2]=0xb, and p[3]=0xa.

If you arrange your binary values posted in your post with this in mind, you might see how it works (..its backward).
Lead Programmer for BrokenThorn Entertainment, Co.
Website: http://www.brokenthorn.com
Email: webmaster@brokenthorn.com

AndyB
Posts:3
Joined:Thu Feb 10, 2011 11:27 am

Re: Bitwise Operations in Physical Memory Manager

Post by AndyB » Thu Feb 10, 2011 8:14 pm

Ahh I had not even considered the endianness of the unsigned int.

Ok, so say if I set the 10th bit in the second array index (map[1]). It will still be the 42nd bit.

Am I right in saying that array indexes/values are still linear in memory it is just the values that are read right to left?

Code: Select all

              map[0]                                map[1]
0000 0000 0000 0000 0000 0000 0000 0000 ... 0000 0000 0000 0000 0000 0010 0000 0000
The bit set as 1 there is the 42nd bit in the memory map that represents the 42nd block of memory?

Andy

halofreak1990
Posts:92
Joined:Thu May 27, 2010 8:54 pm
Location:Netherlands

Re: Bitwise Operations in Physical Memory Manager

Post by halofreak1990 » Fri Feb 11, 2011 12:25 am

AndyB wrote:Am I right in saying that array indexes/values are still linear in memory it is just the values that are read right to left?

Code: Select all

              map[0]                                map[1]
0000 0000 0000 0000 0000 0000 0000 0000 ... 0000 0000 0000 0000 0000 0010 0000 0000
The bit set as 1 there is the 42nd bit in the memory map that represents the 42nd block of memory?

Andy
That is correct.
When setting bits, you need to count from right to left, starting at 0

Post Reply