{"id":13413486,"url":"https://github.com/icza/bitio","last_synced_at":"2025-04-13T02:18:54.352Z","repository":{"id":45591359,"uuid":"60078594","full_name":"icza/bitio","owner":"icza","description":"Optimized bit-level Reader and Writer for Go.","archived":false,"fork":false,"pushed_at":"2023-03-30T13:00:41.000Z","size":65,"stargazers_count":237,"open_issues_count":1,"forks_count":24,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-07-31T20:52:23.098Z","etag":null,"topics":["bit","reader","writer"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/icza.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null},"funding":{"github":"icza"}},"created_at":"2016-05-31T10:02:30.000Z","updated_at":"2024-06-19T09:58:20.000Z","dependencies_parsed_at":"2024-01-08T15:34:43.690Z","dependency_job_id":null,"html_url":"https://github.com/icza/bitio","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/icza%2Fbitio","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/icza%2Fbitio/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/icza%2Fbitio/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/icza%2Fbitio/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/icza","download_url":"https://codeload.github.com/icza/bitio/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248654308,"owners_count":21140281,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bit","reader","writer"],"created_at":"2024-07-30T20:01:41.471Z","updated_at":"2025-04-13T02:18:54.328Z","avatar_url":"https://github.com/icza.png","language":"Go","readme":"# bitio\n\n![Build Status](https://github.com/icza/bitio/actions/workflows/go.yml/badge.svg)\n[![Go Reference](https://pkg.go.dev/badge/github.com/icza/bitio.svg)](https://pkg.go.dev/github.com/icza/bitio)\n[![Go Report Card](https://goreportcard.com/badge/github.com/icza/bitio)](https://goreportcard.com/report/github.com/icza/bitio)\n[![codecov](https://codecov.io/gh/icza/bitio/branch/master/graph/badge.svg)](https://codecov.io/gh/icza/bitio)\n\nPackage `bitio` provides an optimized bit-level `Reader` and `Writer` for Go.\n\nYou can use `Reader.ReadBits()` to read arbitrary number of bits from an `io.Reader` and return it as an `uint64`,\nand `Writer.WriteBits()` to write arbitrary number of bits of an `uint64` value to an `io.Writer`.\n\nBoth `Reader` and `Writer` also provide optimized methods for reading / writing\n1 bit of information in the form of a `bool` value: `Reader.ReadBool()` and `Writer.WriteBool()`.\nThese make this package ideal for compression algorithms that use [Huffman coding](https://en.wikipedia.org/wiki/Huffman_coding) for example,\nwhere decision whether to step left or right in the Huffman tree is the most frequent operation.\n\n`Reader` and `Writer` give a _bit-level_ view  of the underlying `io.Reader` and `io.Writer`, but they also\nprovide a _byte-level_ view (`io.Reader` and `io.Writer`) at the same time. This means you can also use\nthe `Reader.Read()` and `Writer.Write()` methods to read and write slices of bytes. These will give\nyou best performance if the underlying `io.Reader` and `io.Writer` are aligned to a byte boundary\n(else all the individual bytes are assembled from / spread to multiple bytes). You can ensure\nbyte boundary alignment by calling the `Align()` method of `Reader` and `Writer`. As an extra,\n`io.ByteReader` and `io.ByteWriter` are also implemented.\n\n### Bit order\n\nThe more general highest-bits-first order is used. So for example if the input provides the bytes `0x8f` and `0x55`:\n\n    HEXA    8    f     5    5\n    BINARY  1000 1111  0101 0101\n            aaaa bbbc  ccdd dddd\n\nThen ReadBits will return the following values:\n```golang\nr := NewReader(bytes.NewBuffer([]byte{0x8f, 0x55}))\na, err := r.ReadBits(4) //   1000 = 0x08\nb, err := r.ReadBits(3) //    111 = 0x07\nc, err := r.ReadBits(3) //    101 = 0x05\nd, err := r.ReadBits(6) // 010101 = 0x15\n```\n\nWriting the above values would result in the same sequence of bytes:\n```golang\nb := \u0026bytes.Buffer{}\nw := NewWriter(b)\nerr := w.WriteBits(0x08, 4)\nerr = w.WriteBits(0x07, 3)\nerr = w.WriteBits(0x05, 3)\nerr = w.WriteBits(0x15, 6)\nerr = w.Close()\n// b will hold the bytes: 0x8f and 0x55\n```\n### Error handling\n\nAll `ReadXXX()` and `WriteXXX()` methods return an error which you are expected to handle.\nFor convenience, there are also matching `TryReadXXX()` and `TryWriteXXX()` methods\nwhich do not return an error. Instead they store the (first) error in the\n`Reader.TryError` / `Writer.TryError` field which you can inspect later.\nThese `TryXXX()` methods are a no-op if a `TryError` has been encountered before,\nso it's safe to call multiple `TryXXX()` methods and defer the error checking.\n\nFor example:\n```golang\nr := NewReader(bytes.NewBuffer([]byte{0x8f, 0x55}))\na := r.TryReadBits(4) //   1000 = 0x08\nb := r.TryReadBits(3) //    111 = 0x07\nc := r.TryReadBits(3) //    101 = 0x05\nd := r.TryReadBits(6) // 010101 = 0x15\nif r.TryError != nil {\n    // Handle error\n}\n```\nThis allows you to easily convert the result of individual `ReadBits()`, like this:\n```golang\nr := NewReader(bytes.NewBuffer([]byte{0x8f, 0x55}))\na := byte(r.TryReadBits(4))   //   1000 = 0x08\nb := int32(r.TryReadBits(3))  //    111 = 0x07\nc := int64(r.TryReadBits(3))  //    101 = 0x05\nd := uint16(r.TryReadBits(6)) // 010101 = 0x15\nif r.TryError != nil {\n    // Handle error\n}\n```\nAnd similarly:\n```golang\nb := \u0026bytes.Buffer{}\nw := NewWriter(b)\nw.TryWriteBits(0x08, 4)\nw.TryWriteBits(0x07, 3)\nw.TryWriteBits(0x05, 3)\nw.TryWriteBits(0x15, 6)\nif w.TryError != nil {\n    // Handle error\n}\nerr = w.Close()\n// b will hold the bytes: 0x8f and 0x55\n```\n### Number of processed bits\n\nFor performance reasons, `Reader` and `Writer` do not keep track of the number of read or written bits.\nIf you happen to need the total number of processed bits, you may use the `CountReader` and `CountWriter` types\nwhich have identical API to that of `Reader` and `Writer`, but they also maintain the number of processed bits\nwhich you can query using the `BitsCount` field.\n\n### LICENSE\n\nLicensed under either of\n\n- [Apache License, Version 2.0](LICENSE-APACHE)\n- [GNU LESSER GENERAL PUBLIC LICENSE, Version 2.1](LICENSE-LGPL-v2.1)\n\nat your option.\n","funding_links":["https://github.com/sponsors/icza"],"categories":["Microsoft Office","Miscellaneous","Uncategorized","雜項","\u003cspan id=\"其他-miscellaneous\"\u003e其他 Miscellaneous\u003c/span\u003e","其他杂项","杂项","其他"],"sub_categories":["Uncategorized","Advanced Console UIs","Strings","高級控制台界面","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e","暂未分类","未分类的","暂未分类这些库被放在这里是因为其他类别似乎都不适合。","交流","高级控制台界面"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ficza%2Fbitio","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ficza%2Fbitio","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ficza%2Fbitio/lists"}