S
S
soofftt912016-07-07 00:16:26
JavaScript
soofftt91, 2016-07-07 00:16:26

How to decode flags from VK API?

I make a Long Poll request to VK, in response I receive JSON with incoming messages. This json contains a number which is the sum of the codes of the following parameters:
1d954319fcb8454fa7aa5a77e7cfaf04.png
This number needs to be decoded. This is the first time I have encountered such a problem, and the only thing that comes to mind is to calculate the sum of combinations of parameters in advance. Maybe there is a more graceful solution?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lander, 2016-07-07
@soofftt91

if (x & 512) {
  //флаг MEDIA стоит
} else {
  //флаг MEDIA сброшен
}

Same with other flags: 2 - for OUTBOX, 4 - for REPLIED, etc. d.

P
prikazchikof, 2016-07-07
@prikazchikof

You can apply the "greedy" algorithm. The bottom line is this: at each iteration, we select the maximum power of two, less than or equal to the current number. We remember it as one of the terms and subtract from the number. We repeat until the number becomes 0.
Example: expand the number 123.
The maximum power of two, less than or equal to 123 - 64. Remember 64, subtract it from 123, we get 59. The
maximum power of two, less than or equal to 59 - 32 Remember 32, subtract it from 59, we get 27. The
maximum power of two less than or equal to 27 - 16. Remember 16, subtract it from 27, we get 11. The
maximum power of two, less than or equal to 11 - 8. Remember 8, subtract it from 11, we get 3.
The maximum power of two less than or equal to 3 - 2. Remember 2, subtract it from 11, we get 1.
The maximum power of two, less than or equal to 1 - 1. Remember 1, subtract it from 1, we get 0.
The algorithm has completed its work, as a result 123 = 64 + 32 + 16 + 8 + 2 + 1.
Source: http://ru.stackoverflow.com/questions/334200/Algor...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question