Git Product home page Git Product logo

bitsfields.jl's Introduction

BitsFields.jl

Use adjacent bits as a named bitfield. Use multiple bitfields in distinct roles or as primary elements.

Copyright © 2018-2022 by Jeffrey Sarnoff.     This work has been released under The MIT License.


Build Status      


Purpose

This package provides an easy way to describe and use bitfields within Julia.

Example

We want two bitfields, one that is six bits wide and another that is ten bits wide. The total span for both bitfields is 6+10 == 16 bits, so a UInt16 will hold them.

using BitsFields

field1span  =  6
field1shift =  0
field2span  = 10
field2shift = field1span

field1 = BitField(UInt16, :field1, field1span, field1shift)
field2 = BitField(UInt16, :field2, field2span, field2shift)

bitfields = BitFields(field1, field2)

To use the bitfields, provide a referenceable, type-matched and zeroed carrier.

carrier = zero(bitfields)

Now we can set the fields and get their values.

field1value = 0x15
field2value = 0x02f6

set!(bitfields[1], field1value, carrier)
set!(bitfields[2], field2value, carrier)

get(bitfields[2], carrier)  # UInt16(0x02f6)

get(bitfields, carrier)     # [ UInt16(0x15), UInt16(0x02f6) ]

A bitfield may be changed, just set! it again.

Demonstration

Each BitField embedded together as a sequence of BitFields share an unsigned integer bitstype. All fields must specify the same unsigned type, as above where all six bitfields specify UInt64. Any attempt to use more than one Unsigned type as "the mix UInts of different sizes is rejected. So are attempts to use anything other than an unsigned bitstype for BitField embedding.

UInt64 is used for this when there is no unsigned type specified. So you do not need to write it.

using BitsFields



#                    span      shift      name         bits used
bitfield1 = BitField(  8,            0, :field1);   #      8
bitfield2 = BitField(  4,            8, :field2);   #     12
bitfield3 = BitField( 12,          4+8, :field3);   #     24
bitfield4 = BitField( 16,       12+4+8, :field4);   #     40
bitfield5 = BitField( 20,    16+12+4+8, :field5);   #     60
bitfield6 = BitField(  4, 20+16+12+4+8, :field6);   #     64

bitfields = BitFields(bitfield1, bitfield2, bitfield3,
                      bitfield4, bitfield5, bitfield6);

namedfields = NamedTuple(bitfields)

namedfields.field1 === bitfields[1]
namedfields.field2 === bitfields[2]
namedfields.field3 === bitfields[3]
namedfields.field4 === bitfields[4]
namedfields.field5 === bitfields[5]
namedfields.field6 === bitfields[6]

Use Case

This worked example uses bit fields to access parts of floats in accord with the IEEE 754-2008 Floating Point Standard.

bitsfields.jl's People

Contributors

jeffreysarnoff avatar juliatagbot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.