# Getting X bits from an integer?

• I'm looking for an efficient way to get an integer equal to a certain range of bits in another integer in PHP. For example, If I have integer \$x, which is equal to 11, and I wanted to get bits 0 to 3, since 10=0x1011, then bits 0 to 3 will be 0x011==3. How do I do it?

• Doesn't PHP have bitshift operators?

• PHP does have bitwise opperaters ... language.operators.bitwise.php

• Hell, you don't even have to use bitshifters.

You just have to mask the original int with code - 1[/code]

In the case of 11 and 3, it'll become:

1011 & 0111 = 011 == 3

```var decimal = 11;
var taillength = 3;

var clippedDecimal = decimal & (Math.pow(2,taillength) - 1);```

• If you really want bits 0 to 3, that means you want four bits. I am going to assume you want bits 0 to 2 since your example is for three bits:

```\$x = 11;
\$bits = 3;

\$result = \$x & ((1 << \$bits) - 1);
```

• @dhromed said:

Hell, you don't even have to use bitshifters.

You just have to mask the original int with code - 1[/code]

In the case of 11 and 3, it'll become:

1011 & 0111 = 011 == 3

`var decimal = 11;var taillength = 3;var clippedDecimal = decimal & (Math.pow(2,taillength) - 1);`

A bitshift is a single instruction on most processors. pow() on the other hand is many, many floating point operations. Take a look at how it works sometime, then look at the exp and log functions it's based on... not pretty.

The lack of shift operators was a real annoyance for me back when I was a kid using QBasic... more than one of my programs would start with something like FOR B=0 TO 7: BIT(B)=2^B: NEXT because exponentiation was so incredibly slow (you could do it maybe 1000 times per second)

• A bitshift is a single instruction on most processors. pow() on the
other hand is many, many floating point operations. Take a look at how
it works sometime, then look at the exp and log functions it's based
on... not pretty.

Um... dhromed was joking or trolling, obviously. There's no way a programmer can possibly believe that a heavy floating-point math operation should ever be used in the context of bit manipulations on integers!

... well, he was, wasn't he??

• @Khim said:

A bitshift is a single instruction on most processors. pow() on the
other hand is many, many floating point operations. Take a look at how
it works sometime, then look at the exp and log functions it's based
on... not pretty.

Um... dhromed was joking or trolling, obviously. There's no way a programmer can possibly believe that a heavy floating-point math operation should ever be used in the context of bit manipulations on integers!

... well, he was, wasn't he??

Aheh.
*blush*

I knew that each right/left shift is an effective division/multiplication by 2, but was apparently bit- and math-n00bish enough to not make the connection with Math.pow(2).

I have never before in my life fucked around with bitwise operators. The above thing has been my formal deflowering in the world of those operators. My background in CS is nil, nothing and jack-all. As a wholly autodidact, it's possible to make unexpected WTFs at times, because the foundation of the knowledge was never laid properly.

Of course, now that I see it, I'm like DUH.

Do give credit for the fact that my solution is 100% the same as yours, just less efficiently implemented on 1 point due to lack of operator knowledge.

• I have never before in my life fucked around with bitwise operators.
The above thing has been my formal deflowering in the world of those
operators. My background in CS is nil, nothing and jack-all. As a
wholly autodidact, it's possible to make unexpected WTFs at times,
because the foundation of the knowledge was never laid properly.

Of course, now that I see it, I'm like DUH.

Do give credit for the fact that my solution is 100% the same as yours, just less efficiently implemented on 1 point due to lack of operator knowledge.

Yes, of course you are right. There's every reason to respect someone who learns stuff on his own. This place may be a bit too harsh a way of learning new stuff, but it probably works.

I do recall how I not too long ago suggested a less than efficient approach to someone who needed to match IP address prefixes in a language that didn't have bitwise operators - Lotus Domino, I think. (At least he said it didn't have 'em) The solution was using modulo for masking. Add pow() (or whatever equivalent is available) and you have a solution that will look weird to just about everyone. Just do

```result = x mod pow(2, bits)
```

Of course, if you want to extract an address prefix, just subtract:

```prefix = address - (address mod pow(2, bits))
```

It's a kind of forced WTFery, really.

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.