Elmar Naude - 9 months ago 73

PHP Question

Is it possible to get a subsection of a hex value? For example:

`0x7dd becomes 0xdd`

Or even to get a subsection of binary value. For example:

`$hex = 0x7dd;`

$bin = hex2bin($hex);

$ bin = substr($bin,4); //so it excludes the four most significant bits ie. the most significant hex digit

Does anybody know of a efficient way of oing this?

Answer Source

Please read on bitwise operators.

Applying `&`

(bitwise and) on a mask, or `>>`

(right-shift) could be what your looking for (without converting back and forth to `string`

).

For example:

```
0x7dd & 0xff == 0xdd
```

where `& 0xff`

essentially ignores everything except the last two hex digits