# Getting the n-th bit of a integer

**URL:** <https://discuss.ocaml.org/t/getting-the-n-th-bit-of-a-integer/8662>\
**Category:** Learning\
**Tags:** help\
**Created:** [October 20, 2021, 2:34pm UTC](https://discuss.ocaml.org/t/getting-the-n-th-bit-of-a-integer/8662 "2021-10-20T14:34:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gaspardousson](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/gaspardousson/32/3297_2.png) [@gaspardousson](https://discuss.ocaml.org/u/gaspardousson)\
**Post date:** [October 20, 2021, 2:34pm UTC](https://discuss.ocaml.org/t/getting-the-n-th-bit-of-a-integer/8662/1 "2021-10-20T14:34:56Z")

</div>

Hello there,

Do you know any way to get the nth bit of an integer?

Obviously I can (and I do) use “Int.shift\_right” and “mod 2”, but it is probably not the best way to do so, unless the compiler is able to handle such an operation well.

---

<div class="post-metadata">

**Author:** ![ttamttam](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ttamttam/32/132_2.png) [@ttamttam](https://discuss.ocaml.org/u/ttamttam)\
**Post date:** [October 20, 2021, 2:50pm UTC](https://discuss.ocaml.org/t/getting-the-n-th-bit-of-a-integer/8662/2 "2021-10-20T14:50:13Z")

</div>

(Edited by hand, because the link was dropped during the e-mail transmission. Sorry)

Here is a nice reference (Not related to OCaml) that may help:

[http://graphics.stanford.edu/~seander/bithacks.html](http://graphics.stanford.edu/~seander/bithacks.html)

Best regards

---

<div class="post-metadata">

**Author:** ![silene](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/silene/32/2707_2.png) [@silene](https://discuss.ocaml.org/u/silene)\
**Post date:** [October 20, 2021, 4:29pm UTC](https://discuss.ocaml.org/t/getting-the-n-th-bit-of-a-integer/8662/3 "2021-10-20T16:29:59Z")

</div>

> [@gaspardousson](#):
>
> Obviously I can (and I do) use “Int.shift\_right” and “mod 2”, but it is probably not the best way to do so, unless the compiler is able to handle such an operation well.

You should use `land 1` instead of `mod 2`. Other than that, there is not much you can do to improve the generated code.

---

<div class="post-metadata">

**Author:** ![xavierleroy](https://avatars.discourse-cdn.com/v4/letter/x/a9adbd/32.png) [@xavierleroy](https://discuss.ocaml.org/u/xavierleroy)\
**Post date:** [October 20, 2021, 5:07pm UTC](https://discuss.ocaml.org/t/getting-the-n-th-bit-of-a-integer/8662/4 "2021-10-20T17:07:44Z")

</div>

> [@silene](#):
>
> You should use `land 1` instead of `mod 2`

Agreed. One reason is that `x mod 2` can return `-1` if `x` is negative…

If you want the bit as a Boolean instead of an integer, `((x lsr n) land 1) <> 0` should work fine.

---

<div class="post-metadata">

**Author:** ![arbipher](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/arbipher/32/3544_2.png) [@arbipher](https://discuss.ocaml.org/u/arbipher)\
**Post date:** [October 20, 2021, 5:10pm UTC](https://discuss.ocaml.org/t/getting-the-n-th-bit-of-a-integer/8662/5 "2021-10-20T17:10:23Z")

</div>

Prepare one mask value for each n and then bitwise-and the mask with your int.

---

<div class="post-metadata">

**Author:** ![mro](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/mro/32/2496_2.png) [@mro](https://discuss.ocaml.org/u/mro)\
**Post date:** [October 21, 2021, 6:26am UTC](https://discuss.ocaml.org/t/getting-the-n-th-bit-of-a-integer/8662/6 "2021-10-21T06:26:06Z")

</div>

Have you measured the relevance of further performance improvement? Can you provide the figures?

And what do you mean by ‘well’?
