D
D
des1roer2015-08-26 11:22:13
PHP
des1roer, 2015-08-26 11:22:13

bitmask in php

there is a desire to store boolean values ​​as a bitmask.
for example, yes-no-yes will be 1-0-1
as I understand it, you can store something like this 2^1 + 2^0 + 2^3 = 2+1+8=11
I can’t find an example. and is it worth bothering and is it easier to store as 101 and stupidly pull out the element by number in the tuple? the variant with powers of two seems to allow an unlimited increase in the length of the tuple. the truth and complexity of decoding is higher.
----

$store = 0;
$n = 2;
$store = $store | (1<<$n);
$n = 4;
$store = $store | (1<<$n);
var_dump($store);

on the output of int (20)
how now to understand which bit is written?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Martyanov, 2015-08-26
@des1roer

What do you want an example of? Writing the nth bit into a number: store = store | (1<<n), given that the bits are counted from zero.

A
Andrey Pavlenko, 2015-08-26
@Akdmeh

habrahabr.ru/post/134557
here is an article that answers all your questions

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question