oschulz / bitoperations.jl Goto Github PK
View Code? Open in Web Editor NEWJulia Bit Manipulation Functions
License: Other
Julia Bit Manipulation Functions
License: Other
High time to finally register this package.
v0.5:
@inline bflip(x::Integer, bit::Integer) = x $ bmask(typeof(x), bit)
@inline bflip{U <: Integer}(x::Integer, bits::UnitRange{U}) = x $ bmask(typeof(x), bits))
v0.6:
@inline bflip(x::Integer, bit::Integer) = xor(x, bmask(typeof(x), bit))
@inline bflip{U <: Integer}(x::Integer, bits::UnitRange{U}) = xor(x, bmask(typeof(x), bits))
bit
, bits
, nbits
args are Int32 (or Int16 or Int8).On a 64bit [32bit] system, bmask(Int32, 5)
is bmask(Int32, 5::Int64)
[bmask(Int32, 5::32)
].
# using bit::Int32
julia> code_llvm(bmask, (Type{Int32}, Int32))
define i32 @julia_bmask_62946(%jl_value_t*, i32) #0 !dbg !5 {
top:
%2 = call i32 @"julia_<<_62947"(i32 1, i32 %1) #0
ret i32 %2
}
julia> code_native(bmask, (Type{Int32}, Int32))
.text
Filename: bitops.jl
pushq %rbp
movq %rsp, %rbp
Source line: 32
movabsq $"<<", %rax
movl $1, %edi
callq *%rax
popq %rbp
retq
nopw (%rax,%rax)
# using bit::Int64
julia> code_llvm(bmask, (Type{Int32}, Int64))
define i32 @julia_bmask_62948(%jl_value_t*, i64) #0 !dbg !5 {
top:
%2 = icmp slt i64 %1, 0
%3 = trunc i64 %1 to i32
%4 = shl i32 1, %3
%5 = icmp ugt i64 %1, 31
%6 = select i1 %5, i32 0, i32 %4
%7 = sub i64 0, %1
%8 = trunc i64 %7 to i32
%9 = lshr i32 1, %8
%10 = icmp ugt i64 %7, 31
%11 = select i1 %10, i32 0, i32 %9
%12 = select i1 %2, i32 %11, i32 %6
ret i32 %12
}
julia> code_native(bmask, (Type{Int32}, Int64))
.text
Filename: bitops.jl
pushq %rbp
movq %rsp, %rbp
Source line: 32
movl $1, %eax
movl $1, %edx
movl %esi, %ecx
shll %cl, %edx
xorl %edi, %edi
cmpq $31, %rsi
cmoval %edi, %edx
movq %rsi, %rcx
negq %rcx
shrl %cl, %eax
cmpq $31, %rcx
cmoval %edi, %eax
testq %rsi, %rsi
cmovnsl %edx, %eax
popq %rbp
retq
nopw %cs:(%rax,%rax)
This issue is used to trigger TagBot; feel free to unsubscribe.
If you haven't already, you should update your TagBot.yml
to include issue comment triggers.
Please see this post on Discourse for instructions and more details.
If you'd like for me to do this for you, comment TagBot fix
on this issue.
I'll open a PR within a few hours, please be patient!
First issue: arrays start at one in julia, but using a range starting with one with bitops returns a range starting with the second bit.
julia> bits = 0b1010100010101100
0xa8ac
julia> bget(bits, 1:4)
0x0006
julia> bget(bits, 0:4)
0x000c
julia> (bitstring(bget(bits, 1:4)), bitstring(bget(bits, 0:4)))
("0000000000000110", "0000000000001100")
Additionally, StepRanges (declared with start:step:stop
) do not work. This would be useful to have properly working, for xample with a backwards-stepped range, such as 16:-1:0
julia> bget(bits, 1:2:4)
ERROR: MethodError: no method matching bget(::UInt16, ::StepRange{Int64, Int64})
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.