Jump to content
  • Advertisement
Sign in to follow this  
namron2

Masking??

This topic is 3941 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am probably way off base here in this forum but here goes anyway. I am trying to use a program which accesses offsets in flight sim and, one of the instructions to identify the bit number is to mask with 2**n??? I am lost as I don't know what this means. An example is to use an offset with a memory address and manipulate bit number 3. In this example bit # 3, using the mask described above means 8?. How does this 2**n work ?? Any help appreciated Norm

Share this post


Link to post
Share on other sites
Advertisement
(I don't know how much you know already, so hope this isn't too patronising).

I assume that 2**x means in this instance "two-to-the-power-of-x". This can also be expressed as 1<<x due to the binary representation of numbers.

To put this into English; in decimal (base ten), every time you move a number to the left it becomes ten times as large; thus 1 becomes 10 (it's been shifted left one place), 10 becomes 100, 100 becomes 1000 and so on. This matches the pattern of taking 10**x.

In binary (base two) the same thing happens, but it becomes multiplied by two (so 1<<1 = 2, 2<<1 = 4, 4<<1 = 8 and so on). You can see this by converting to binary: %0001<<1 = %0010, %0010<<1 = %0100.

As you can see, if you start with 1 and shift it left by x - which is the same trick as taking two-to-the-power-of-x - the number only has a single bit set. You can use this to mask a bitfield using the bitwise and operator (&).

In other words;

function GetBit(int value, int index) {
return (value & (1 << index)) != 0;
}

Share this post


Link to post
Share on other sites
2**n means 2^n. I think the notation started on computers keyboards that didn't have the caret.

But just know that it means 2^n, thus, bit 0 is 2^0==1, bit 1 is 2^1==2, and on and on.

Share this post


Link to post
Share on other sites
Quote:
Original post by aCynic2
2**n means 2^n. I think the notation started on computers keyboards that didn't have the caret.

But just know that it means 2^n, thus, bit 0 is 2^0==1, bit 1 is 2^1==2, and on and on.


C++ the language uses the caret for the XOR operator which has entirely different behavior from taking the power of a number. So it's not just for keyboards without the caret symbol.

edit: I missed the word 'started' in the post so that could actually be true.

Share this post


Link to post
Share on other sites

Thank you to all who replied. I really appreciate the time taken to explain this to me.
My question has been answered and it makes sense now.

Norm

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!