@JeffreySarnoff, printing pretty bits seems to be a good candidate for the awesome PrettyTables.jl package. Not sure if you like it but, see herein a basic pretty_bits()
function, that hopefully is also correct.
PS: further edited after feedback from @JeffreySarnoff
using PrettyTables
function pretty_bits(x)
bx = reverse(bitstring(x))
N = length(bx);
Nc = min(16, N) # max columns to display = 16 bits
Nr = N÷Nc
data = [parse(Int,bx[Nc*i+j]) for i in 0:Nr-1, j in 1:Nc][:,end:-1:1]
hl1 = Highlighter((data,i,j) -> data[i,j]==1, crayon"red bold")
hl2 = Highlighter((data,i,j) -> data[i,j]==0, crayon"cyan bold")
if Nr>1
pretty_table(data, [x for x = Nc-1:-1:0], header_crayon = crayon"light_gray bold italics",
vlines = collect(1:4:Nc),
row_names = [string(Nc*i-1," - ",Nc*(i-1)) for i in 1:Nr],
row_name_alignment = :c,
formatters = ft_printf("%i",1:Nc), highlighters=(hl1,hl2))
else
pretty_table(data, [x for x = Nc-1:-1:0], header_crayon = crayon"light_gray bold italics",
vlines = collect(4:4:Nc-4),
formatters = ft_printf("%i",1:Nc), highlighters=(hl1,hl2))
end
end
x = 100 # Int64
pretty_bits(Int8(x))
pretty_bits(Int16(x))
pretty_bits(Int32(x))
pretty_bits(x) # Int64
pretty_bits(Int128(x))
pretty_bits(convert(UInt8,x)) # 0x64
pretty_bits(convert(UInt32,x)) # 0x00000064
A more compact variant, pretty_bits2()
, produces:
function pretty_bits2(x)
bx = reverse(bitstring(x))
N = length(bx);
Nc = min(16, N) # max columns to display = 16 bits
Nr = N÷Nc
data = [bx[Nc*i+j:Nc*i+j+3] for i in 0:Nr-1, j in 1:4:Nc][:,end:-1:1]
hl1 = Highlighter((data,i,j) -> occursin("1",data[i,j]), crayon"red bold")
hl2 = Highlighter((data,i,j) -> !occursin("1",data[i,j]), crayon"cyan")
if Nr>1
pretty_table(data, ["$x-$y" for (x,y) in zip(Nc-1:-4:0,Nc-4:-4:0)],
header_crayon = crayon"light_gray bold italics",
vlines = [1], row_name_alignment = :c,
row_names = [string(Nc*i-1," - ",Nc*(i-1)) for i in 1:Nr],
formatters = ft_printf("%i",1:Nc), highlighters=(hl1,hl2))
else
pretty_table(data, ["$x-$y" for (x,y) in zip(Nc-1:-4:0,Nc-4:-4:0)],
vlines = :none, header_crayon = crayon"light_gray bold italics",
formatters = ft_printf("%i",1:Nc), highlighters=(hl1,hl2))
end
end
x = 100 # Int64
pretty_bits2(Int8(x))
pretty_bits2(Int16(x))
pretty_bits2(Int32(x))
pretty_bits2(x) # Int64
pretty_bits2(Int128(x))
pretty_bits2(convert(UInt8,x)) # 0x64
pretty_bits2(convert(UInt32,x)) # 0x00000064