4

In Go is it possible to define a custom type with a number of bits other than those offered by byte uint uint16 or any of the other built-in types?

I'm planning on using "just enough bits" to represent variables and wanted a 6-bit and a 4-bit type. Perhaps a composite bool type?

type fourbit struct{
    ones   bool
    twos   bool
    fours  bool
    eights bool
}

Though this sort of thing is quite messy and it would be nice to have a more general solution for n-bit types.

2
  • You might be looking for something like this; golang.org/pkg/encoding/binary you can store small values as a byte, not so sure about storing values that are one and a half bytes, you'll probably have to round up and put it in a 2byte buffer. Commented Oct 29, 2015 at 22:03
  • A more common pattern in most programming languages (and used throughout Go) is a bitmap to hold up to 8 values per byte: play.golang.org/p/DZj9FerK19 Commented Oct 30, 2015 at 13:49

1 Answer 1

7

No. The minimum size of a Go type in current implementations, including type bool, is one byte, .

References:

The Go Programming Language Specification

Sign up to request clarification or add additional context in comments.

1 Comment

basic addressable unit, interesting, very cool thanks for the quick reply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.