# Making a UInt64 bit-masking function generic

**URL:** https://discourse.julialang.org/t/making-a-uint64-bit-masking-function-generic/3477
**Category:** General Usage
**Created:** [May 2, 2017, 10:15am UTC](https://discourse.julialang.org/t/making-a-uint64-bit-masking-function-generic/3477 "2017-05-02T10:15:57Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Ward9250](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ward9250/32/42768_2.png) [@Ward9250](https://discourse.julialang.org/u/Ward9250)
#### Post date: [May 2, 2017, 10:15am UTC](https://discourse.julialang.org/t/making-a-uint64-bit-masking-function-generic/3477/1 "2017-05-02T10:15:57Z")

</div>

I’ve got a function that currently accepts a 64 bit integer, and masks it with some other 64 bit integer e.g.

`x & 0x3333333333333333`

In such code, the hex is written in full, literally. But if I want to generalise this to other sizes of UInt, how do I specify 0x33 (for UInt8), 0x3333 (UInt16) and so on, without writing a lot of separate methods with different hex literals, with a fair amount of code duplication? Choosing a hex literal during compilation with a generated function seems like overkill?

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [May 2, 2017, 10:22am UTC](https://discourse.julialang.org/t/making-a-uint64-bit-masking-function-generic/3477/2 "2017-05-02T10:22:47Z")

</div>

`x & (0x3333333333333333 % typeof(x))` should work, where `%` converts the hex literal to one of the appropriate type.

---

<div class="post-metadata">

### Author: ![Ward9250](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ward9250/32/42768_2.png) [@Ward9250](https://discourse.julialang.org/u/Ward9250)
#### Post date: [May 2, 2017, 10:24am UTC](https://discourse.julialang.org/t/making-a-uint64-bit-masking-function-generic/3477/3 "2017-05-02T10:24:26Z")

</div>

Will I suffer a performance hit since modulus is quite an expensive operation?

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [May 2, 2017, 10:29am UTC](https://discourse.julialang.org/t/making-a-uint64-bit-masking-function-generic/3477/4 "2017-05-02T10:29:15Z")

</div>

No, that’s not really a modulus – it’s a kind of unsafe `convert(typeof(x), 0x3333333333333333)` call (`Base.trunc_int`, actually).  
The native code seems pretty good to me:

```julia
julia> f{T}(x::T) = x & (0x3333333333333333 % T)
f (generic function with 1 method)

julia> f(UInt8(4))
0x00

julia> @code_native f(UInt8(4))
        .text
Filename: REPL[4]
        pushq %rbp
        movq %rsp, %rbp
Source line: 1
        andb $51, %cl
        movl %ecx, %eax
        popq %rbp
        retq
        nopl (%rax,%rax)

```

and actually is the same as if you write the literal explicitly:

```julia
julia> g(x::UInt8) = x & 0x33
g (generic function with 1 method)

julia> g(UInt8(4))
0x00

julia> @code_native g(UInt8(4))
        .text
Filename: REPL[9]
        pushq %rbp
        movq %rsp, %rbp
Source line: 1
        andb $51, %cl
        movl %ecx, %eax
        popq %rbp
        retq
        nopl (%rax,%rax)

```

---

<div class="post-metadata">

### Author: ![Ward9250](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ward9250/32/42768_2.png) [@Ward9250](https://discourse.julialang.org/u/Ward9250)
#### Post date: [May 2, 2017, 10:51am UTC](https://discourse.julialang.org/t/making-a-uint64-bit-masking-function-generic/3477/5 "2017-05-02T10:51:25Z")

</div>

Thanks @pfitzseb I didn’t know that the `%` operator could be used to do a unsafe conversion.

---

<div class="post-metadata">

### Author: ![fengyang.wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fengyang.wang/32/104_2.png) [@fengyang.wang](https://discourse.julialang.org/u/fengyang.wang)
#### Post date: [May 3, 2017, 7:58am UTC](https://discourse.julialang.org/t/making-a-uint64-bit-masking-function-generic/3477/6 "2017-05-03T07:58:16Z")

</div>

Here’s how I would write it. This works with all `UInt` types, including `UInt128`, which would otherwise require writing more `3`s.

```julia
f(x) = x & div(typemax(x), 5)

```

It seems successfully constant folded so there is no performance impact.

---

<div class="post-metadata">

### Author: ![Ward9250](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ward9250/32/42768_2.png) [@Ward9250](https://discourse.julialang.org/u/Ward9250)
#### Post date: [May 3, 2017, 1:44pm UTC](https://discourse.julialang.org/t/making-a-uint64-bit-masking-function-generic/3477/7 "2017-05-03T13:44:23Z")

</div>

Is there a trick to knowing what to you need instead of “5” if you wanted to use any other bit pattern?

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [May 3, 2017, 2:13pm UTC](https://discourse.julialang.org/t/making-a-uint64-bit-masking-function-generic/3477/8 "2017-05-03T14:13:44Z")

</div>

The `5` mostly boils down to

```julia
julia> 0x33//0xff
0x01//0x05

```

but constant folding works equally well for

```julia
f(x) = x & (div(typemax(x), 0xff) * 0x33)

```

if you want it less magic.

---

<div class="post-metadata">

### Author: ![Ward9250](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ward9250/32/42768_2.png) [@Ward9250](https://discourse.julialang.org/u/Ward9250)
#### Post date: [May 3, 2017, 2:24pm UTC](https://discourse.julialang.org/t/making-a-uint64-bit-masking-function-generic/3477/9 "2017-05-03T14:24:11Z")

</div>

I think I can see how this works in general cases - hopefully I’m right: The div of 0xfff…(until the end of the int) by the byte 0xff, creates bytes of ‘1’ throughout the integer. Which when multiplied by the byte which is your desired pattern, applies this pattern throughout the integer.
