# UInt8 access Bits?

**URL:** https://discourse.julialang.org/t/uint8-access-bits/43990
**Category:** New to Julia
**Created:** [July 30, 2020, 4:32pm UTC](https://discourse.julialang.org/t/uint8-access-bits/43990 "2020-07-30T16:32:56Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![YoungAlpha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/youngalpha/32/16717_2.png) [@YoungAlpha](https://discourse.julialang.org/u/YoungAlpha)
#### Post date: [July 30, 2020, 4:32pm UTC](https://discourse.julialang.org/t/uint8-access-bits/43990/1 "2020-07-30T16:32:56Z")

</div>

Hi, guys,  
is there a way to query or flip specific bits?

```julia
julia> binary = 0b111
0x07

julia> bitstring(binary)
"00000111"

julia> typeof(binary)
UInt8

```

In this case the following results would be expected:

```julia
binary[1] == binary[2] == binary[3] == 1

```

and for all all N greater than 3 (within half the limits)

```julia
binary[4] == ... == binary[N] == 0

```

how could I perform such a thing? how could I possibly perform this performant?

thanks.

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [July 30, 2020, 4:38pm UTC](https://discourse.julialang.org/t/uint8-access-bits/43990/2 "2020-07-30T16:38:03Z")

</div>

```julia
a=digits(binary, base=2, pad=8)

```

stolen from here:

> [@Convert integer to bits array](https://discourse.julialang.org/t/convert-integer-to-bits-array/26663/4):
>
> digits(x, base=2, pad=16) |\> reverse

---

<div class="post-metadata">

### Author: ![YoungAlpha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/youngalpha/32/16717_2.png) [@YoungAlpha](https://discourse.julialang.org/u/YoungAlpha)
#### Post date: [July 30, 2020, 4:44pm UTC](https://discourse.julialang.org/t/uint8-access-bits/43990/3 "2020-07-30T16:44:12Z")

</div>

I know that in theory I could just look at the bit shifts, but that sounds unnecessarily complicated. Is there no way to access the memory directly at a location?

> [@oheil](#):
>
> `digits`

here the number is probably converted to an array every time, right? is there maybe a more performant version?

i have to do many lookups for many different numbers and converting the number each time is not performant

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [July 30, 2020, 4:49pm UTC](https://discourse.julialang.org/t/uint8-access-bits/43990/4 "2020-07-30T16:49:25Z")

</div>

You’ll be much better off just using shift and bitwise operation.

`binary & (UInt8(1) << 2)` (access bit) or `binary | (UInt8(1) << 2)` (set bit) or `binary & ~(UInt8(1) << 2)` (unset bit)

---

<div class="post-metadata">

### Author: ![YoungAlpha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/youngalpha/32/16717_2.png) [@YoungAlpha](https://discourse.julialang.org/u/YoungAlpha)
#### Post date: [July 30, 2020, 5:04pm UTC](https://discourse.julialang.org/t/uint8-access-bits/43990/5 "2020-07-30T17:04:43Z")

</div>

So exactly what i wanted to do would be a way to count the number of True bits in a number. would you always shift to the right and check at the first position if the number % 2 == 0 is ergo if or if not the 1 bit is set is a good choice? or how would you do something like that to perform better?

for the example above

```julia
julia> bitstring(binary)
"00000111"

```

i would like to get 3 because there 3 bits set as true

```julia
julia> bitstring(binary2)
"00100000000000000000000100111"

```

would be 5

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [July 30, 2020, 5:09pm UTC](https://discourse.julialang.org/t/uint8-access-bits/43990/6 "2020-07-30T17:09:10Z")

</div>

I’m not sure if that’s just an example — but if that’s exactly what you want there’s a function for it: [`count_ones`](https://docs.julialang.org/en/v1/base/numbers/#Base.count_ones).

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [July 30, 2020, 6:02pm UTC](https://discourse.julialang.org/t/uint8-access-bits/43990/7 "2020-07-30T18:02:03Z")

</div>

Whether it’s just an example or exactly what you want to do, using the bit operations as I showed is exactly how you’d implement it. On top of that, there are tricks/estabilished patterns for almost all of the common operations that you might want and compilers can also do pattern matching for to for further optimizaion.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [July 30, 2020, 6:36pm UTC](https://discourse.julialang.org/t/uint8-access-bits/43990/8 "2020-07-30T18:36:47Z")

</div>

> [@YoungAlpha](#):
>
> I know that in theory I could just look at the bit shifts, but that sounds unnecessarily complicated. Is there no way to access the memory directly at a location?

And somehow I missed that part initially.

1. Bit shift is not complicated, in fact it’s much simpler than accessing memory.
2. There’s no memory to access here, different bits do not have different memory location.

Now if you are talking about syntax only and if you just want to have a shorter syntax to write, and what you mean by accessing memory is the `[]` syntax then sure, you can implement it yourself. And I won’t even bother implementing the `[]` syntax but just a function like `setbit`, `testbit` would work, very similar to what I did [here](https://github.com/JuliaLang/julia/blob/710cf2813affd5124e6bec1b84dcb547b2d619c1/src/processor.cpp#L82-L131). In any case, bit shifting is what you must do (even for the `digits` version, you just do much more useless stuff at the same time) but you can hide that behind whatever syntax you like…

---

<div class="post-metadata">

### Author: ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)
#### Post date: [July 31, 2020, 8:40am UTC](https://discourse.julialang.org/t/uint8-access-bits/43990/9 "2020-07-31T08:40:10Z")

</div>

You can use the lightweight [`Bits`](https://github.com/rfourquet/Bits.jl) package:

```julia
julia> using Bits

julia> binary = bits(0b111)
<00000111>

julia> binary[1] == binary[2] == binary[3] == 1
true

julia> binary[4]
false

julia> collect(binary)
8-element Vector{Bool}:
 1
 1
 1
 0
 0
 0
 0
 0

```
