Public Group

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

## 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 on other sites
(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 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 on other sites
Quote:
 Original post by aCynic22**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 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

1. 1
Rutin
25
2. 2
3. 3
4. 4
5. 5

• 10
• 13
• 19
• 14
• 9
• ### Forum Statistics

• Total Topics
632943
• Total Posts
3009352
• ### Who's Online (See full list)

There are no registered users currently online

×