A
A
Alexey Yarkov2017-08-19 20:52:36
In contact with
Alexey Yarkov, 2017-08-19 20:52:36

How to understand bit masks or whatever they are?

https://vk.com/dev/permissions
I just can’t enter how to do it using such a set of permissions in my API. For example, so that the user can post articles and comment, but cannot delete them. Well, and so on.
Explain how to down or give something to read, please.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2017-08-19
@yarkov

Do you know about the binary representation of numbers?

0 = 0000 0000
1 = 0000 0001
2 = 0000 0010
3 = 0000 0011
4 = 0000 0100
5 = 0000 0101
6 = 0000 0110
7 = 0000 0111
8 = 0000 1000
9 = 0000 1001

... etc. Up to 2 32 or even up to 2 64 - depends on the system, 32 or 64 bit and programming language.
Bit positions are counted from right to left. The rightmost bit has position 0. The bit position is a power of two. If the bit is set to 1, add 2 to the power of that position.
For example, a number 3 = 0000 0011means 2 0 + 2 1 = 1 + 2 = 3.
It is noteworthy that the powers of two - 0, 1, 2, 4, 8, 16, 32, 64, ... - are expressed with just one included bit, one unit , the rest of the bits are zeros.
Bit masks are an arrangement that each bit (each position) means something specific that can be turned on or off, 1 or 0. Like a switch bar .
For example, with VKontakte permissions :
1 - бит 0 - notify
2 - бит 1 - friends
4 - бит 2 - photos
8 - бит 3 - audio

VK has a long line, consisting of 32 "switches".
Let's say your application requires photos and audio permissions - bits 2 and 3 must be set to 1, the rest to 0. This can be done by simple addition: 2 2 + 2 3 = 4+8 = 12. In binary: 12 = 0000 0000 0000 1100
For convenience of calculating VC, write directly the values ​​to add to get the desired bitmask - the final number that you will pass to the API method to request permission.
Another example, you need a wall and offline access at all times. Look in the table for the numbers: wall (+8192) and offline (+65536). So you need to ask permission for mask 73728

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question