{"id":17086546,"url":"https://github.com/arl/bitfield","last_synced_at":"2025-04-12T21:41:16.449Z","repository":{"id":219278220,"uuid":"743126225","full_name":"arl/bitfield","owner":"arl","description":"Go-generate code to emulate C bit fields","archived":false,"fork":false,"pushed_at":"2024-02-05T09:29:10.000Z","size":85,"stargazers_count":7,"open_issues_count":2,"forks_count":1,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-26T15:48:15.918Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/arl.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-01-14T12:17:17.000Z","updated_at":"2024-10-24T12:23:02.000Z","dependencies_parsed_at":"2024-02-05T10:48:54.349Z","dependency_job_id":"836ba7c8-3630-4a70-808f-385f5268c378","html_url":"https://github.com/arl/bitfield","commit_stats":null,"previous_names":["arl/bitfield"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arl%2Fbitfield","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arl%2Fbitfield/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arl%2Fbitfield/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arl%2Fbitfield/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/arl","download_url":"https://codeload.github.com/arl/bitfield/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248637832,"owners_count":21137538,"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":[],"created_at":"2024-10-14T13:28:55.132Z","updated_at":"2025-04-12T21:41:16.428Z","avatar_url":"https://github.com/arl.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Tests](https://github.com/arl/bitfield/actions/workflows/test.yml/badge.svg)](https://github.com/arl/bitfield/actions/workflows/test.yml)\n\nBitfield\n=======\n\nThis is a Go code generation tool to emulate C bitfields and unions.\n\n## Quickstart\n\nInstall with:\n\n```sh\ngo install github.com/arl/bitfield@latest\n```\n\nSay you want to generate, in Go, the following C struct:\n\n```c\nstruct scroll\n{\n    unsigned X : 5;\n    unsigned Y : 5;\n};\n```\n\nYou'd declare the following Go struct in a file called `scroll_gen.go`, only used to define the bit field, that's why it's guarded with an `go:build ignore` tag.\n\n```go\n//go:build ignore\n\npackage mypkg\n\n//go:generate bitfield -out scroll.go\n\ntype Scroll struct {\n\tX uint8 `bitfield:\"5\"`\n\tY uint8 `bitfield:\"5\"`\n}\n```\n\nAnd then run:\n\n```sh\ngo generate scroll_gen.go\n```\n\nThis creates the following `scroll.go`:\n\n```go\ntype Scroll uint16\n\nfunc (s Scroll) X() uint8 {\n\treturn uint8(s \u0026 0x1f)\n}\n\nfunc (s *Scroll) SetX(val uint8) {\n\t*s \u0026^= 0x1f\n\t*s |= Scroll(val \u0026 0x1f)\n}\n\nfunc (s Scroll) Y() uint8 {\n\treturn uint8((s \u003e\u003e 5) \u0026 0x1f)\n}\n\nfunc (s *Scroll) SetY(val uint8) {\n\t*s \u0026^= 0x1f \u003c\u003c 5\n\t*s |= Scroll(val\u00260x1f) \u003c\u003c 5\n}\n```\n\nNote that both getters are defined on the value and setters on the pointer.\n\n\n### Supported field types\n\nThese are the allowed types for the fields of the input struct:\n - bool\n - uint8\n - uint16\n - uint32\n - uint64\n\n`bool` can only be defined on a 1-bit wide field.\n\nFor the generated type, `bitfield` automatically uses the smallest unsigned integer to accomodate for all the bits of the field.\n\n\n### Bit fields with anonymous unions\n\nThe following C union:\n\n```c\nunion Addr\n{\n    struct\n    {\n        unsigned cX : 5;  // Coarse X.\n        unsigned cY : 5;  // Coarse Y.\n        unsigned nt : 2;  // Nametable.\n        unsigned fY : 3;  // Fine Y.\n    };\n    struct\n    {\n        unsigned l : 8;\n        unsigned h : 7;\n    };\n\n    unsigned val : 14;\n};\n```\n\ncan be defined with:\n\n```go\ntype Addr struct {\n\tcX uint8 `bitfield:\"5,union=scroll\"`\n\tcY uint8 `bitfield:\"5,union=scroll\"`\n\tnt uint8 `bitfield:\"2,union=scroll\"`\n\tfY uint8 `bitfield:\"3,union=scroll\"`\n\n\tl uint8 `bitfield:\"8,union=lohi\"`\n\th uint8 `bitfield:\"7,union=lohi\"`\n\n\t// When not specified, implicitely uses the 'default' union.\n\tval uint8 `bitfield:\"14\"`\n}\n```\n\n## Usage\n\n```\nbitfield -h\nUsage of bitfield:\n  -in string\n        INPUT file name (necessary unless within a go:generate comment)\n  -out string\n        output file name (defaults to standard output)\n  -pkg string\n        package name (defaults to INPUT file package)\n  -type string\n        name of the type to convert (defaults to all structs) (default \"all\")\n```\n\n## License\n\nThis project is licensed under the [MIT](LICENSE) - see the [LICENSE](LICENSE) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farl%2Fbitfield","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farl%2Fbitfield","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farl%2Fbitfield/lists"}