# Reading bitarray from disk has ordering flipped in groups of 8, how to fix?

**URL:** <https://discourse.julialang.org/t/reading-bitarray-from-disk-has-ordering-flipped-in-groups-of-8-how-to-fix/36123>\
**Category:** General Usage\
**Tags:** binaryio\
**Created:** [March 18, 2020, 12:24am UTC](https://discourse.julialang.org/t/reading-bitarray-from-disk-has-ordering-flipped-in-groups-of-8-how-to-fix/36123 "2020-03-18T00:24:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tlnagy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlnagy/32/5815_2.png) [@tlnagy](https://discourse.julialang.org/u/tlnagy)\
**Post date:** [March 18, 2020, 12:24am UTC](https://discourse.julialang.org/t/reading-bitarray-from-disk-has-ordering-flipped-in-groups-of-8-how-to-fix/36123/1 "2020-03-18T00:24:21Z")

</div>

On `Julia 1.4.0-rc2` (I don’t think it’s a new behavior), I see the following:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/a/4ac9edbf5c39806340ecb0e880af6955c4898de1.png)

versus the expected:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/b/7bf7b1ff74a900e87c8e9bb5221a453770004c93.png)

I’m reading in the image as follows:

```julia
io = open("bilevel_example.tif")
seek(io, 8)
data = BitArray(undef, 40160*8)

read!(io, data);

```

It appears that the bits are vertically flipped every 8 bits. The file was written and read on a little-endian machine. Is there any way to change this?

Here’s the file in question: [Box](https://ucsf.box.com/s/n6jpmxknsvnvqcjzqlyv8hxti8rfkum0)

---

<div class="post-metadata">

**Author:** ![tlnagy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlnagy/32/5815_2.png) [@tlnagy](https://discourse.julialang.org/u/tlnagy)\
**Post date:** [March 18, 2020, 12:44am UTC](https://discourse.julialang.org/t/reading-bitarray-from-disk-has-ordering-flipped-in-groups-of-8-how-to-fix/36123/2 "2020-03-18T00:44:10Z")

</div>

I believe this is coming from the least significant bit being in the leftmost position of each byte so I need to do a bit swap operation, which AFAIK there isn’t a dedicated Julia function for? (`bswap` swaps bytes)

---

<div class="post-metadata">

**Author:** ![laborg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laborg/32/5474_2.png) [@laborg](https://discourse.julialang.org/u/laborg)\
**Post date:** [March 18, 2020, 5:15am UTC](https://discourse.julialang.org/t/reading-bitarray-from-disk-has-ordering-flipped-in-groups-of-8-how-to-fix/36123/3 "2020-03-18T05:15:50Z")

</div>

Maybe you can copy parts of this code if you aren’t using master: [add `bitreverse` function by JeffBezanson · Pull Request #34791 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/34791)

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [March 18, 2020, 6:22am UTC](https://discourse.julialang.org/t/reading-bitarray-from-disk-has-ordering-flipped-in-groups-of-8-how-to-fix/36123/4 "2020-03-18T06:22:47Z")

</div>

Not really familiar with .tif, but have you tried using Images.jl to read your image? They’re using FileIO.jl in the background which has explicit support for .tif files, maybe they do the flipping already?

Otherwise, maybe `Base.ltoh` can help you.

---

<div class="post-metadata">

**Author:** ![tlnagy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlnagy/32/5815_2.png) [@tlnagy](https://discourse.julialang.org/u/tlnagy)\
**Post date:** [March 18, 2020, 5:03pm UTC](https://discourse.julialang.org/t/reading-bitarray-from-disk-has-ordering-flipped-in-groups-of-8-how-to-fix/36123/5 "2020-03-18T17:03:28Z")

</div>

I’m actually in the process of writing a pure-Julia TIFF reader/writer, which generalizes an earlier package that I created. See [GitHub - tlnagy/TiffImages.jl: Pure-Julia TIFF I/O with a focus on correctness 🧐](https://github.com/tlnagy/TIFF.jl) and [GitHub - tlnagy/OMETIFF.jl: I/O operations for OME-TIFF files in Julia](https://github.com/tlnagy/OMETIFF.jl)

---

<div class="post-metadata">

**Author:** ![tlnagy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlnagy/32/5815_2.png) [@tlnagy](https://discourse.julialang.org/u/tlnagy)\
**Post date:** [March 18, 2020, 5:07pm UTC](https://discourse.julialang.org/t/reading-bitarray-from-disk-has-ordering-flipped-in-groups-of-8-how-to-fix/36123/6 "2020-03-18T17:07:00Z")

</div>

This looks exactly like what I was looking for. I’m targeting 1.4, but I can copy that code into the package until it lands in stable.

---

<div class="post-metadata">

**Author:** ![tlnagy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlnagy/32/5815_2.png) [@tlnagy](https://discourse.julialang.org/u/tlnagy)\
**Post date:** [March 18, 2020, 6:43pm UTC](https://discourse.julialang.org/t/reading-bitarray-from-disk-has-ordering-flipped-in-groups-of-8-how-to-fix/36123/7 "2020-03-18T18:43:43Z")

</div>

Thanks @laborg, that code basically did what I want, I just had get rid of the bswap at the end since the byte order is correct (both little-endian).

```julia
const _mask1_uint128 = (UInt128(0x5555555555555555) << 64) | UInt128(0x5555555555555555)
const _mask2_uint128 = (UInt128(0x3333333333333333) << 64) | UInt128(0x3333333333333333)
const _mask4_uint128 = (UInt128(0x0f0f0f0f0f0f0f0f) << 64) | UInt128(0x0f0f0f0f0f0f0f0f)

function reversebits!(x::BitArray)
    n = length(x.chunks)
    for i in 1:n
        x.chunks[i] = reversebits(x.chunks[i])
    end
end

function reversebits(x::Base.BitInteger)
    z = unsigned(x)
    mask1 = _mask1_uint128 % typeof(z)
    mask2 = _mask2_uint128 % typeof(z)
    mask4 = _mask4_uint128 % typeof(z)
    z = ((z & mask1) << 1) | ((z >> 1) & mask1)
    z = ((z & mask2) << 2) | ((z >> 2) & mask2)
    z = ((z & mask4) << 4) | ((z >> 4) & mask4)
    return z # <----- got rid of the bswap here
end

seek(tf.io.io, 8)
data = BitArray(undef, 40160*8)

read!(tf.io.io, data);
reversebits!(data)

Gray.(reshape(data, 640, :)')

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/b/9/b93d5cc90e530394dc34bbaca3d08b7bbfe3987e.png)
