百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
B

bitfield-c

> 编程语言
开源

C 语言中的位元数组解析和编码实用程序库

100 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

C 语言中的位元数组解析和编码实用程序库

Bitfield Utilities in C

This is a C library with functions to help encode and decode Controller Area Network (CAN) message payloads or other bitfields.

The header files contain complete function documentation, but to get you started, here are examples using the API:

Bitfield Manipulation

The bitfields are stored in uint8_t[].

c
uint8_t data[4] = {0x12, 0x34, 0x56, 0x78};
uint8_t result = get_byte(data, sizeof(data), 0);
// result = 0x12;
result = get_nibble(data, sizeof(data), 0);
// result = 0x1;
bool success = copy_bits_right_aligned(data, 4, 4, 12, result, 4)
// success == true
// result[0] == 0x2
// result[1] == 0x34

8 Byte Helpers

If you are dealing with 8 byte CAN messages as uint64_t, there are some additional functions prefixed with eightbyte_ that may be faster or more useful.

8 Byte Decoding

c
uint64_t data = 0x8000000000000000;
uint64_t result = eightbyte_get_bitfield(data, 0, 1, false);
// result == 0x1

data = 0x0402574d555a0401;
result = eightbyte_get_bitfield(data, 16, 32, false);
// result = 0x574d555a;

data = 0x00000000F34DFCFF;
result = eightbyte_get_byte(data, 0, false);
//result = 0x0

result = eightbyte_get_byte(data, 4, false);
//result = 0xF3

result = eightbyte_get_nibble(data, 10, false);
//result = 0x4;

8 Byte Encoding

c
uint64_t data = 0;
fail_unless(8byte_set_bitfield(1, 0, 1, &data));
uint64_t result = eightbyte_get_bitfield(data, 0, 1, false);
ck_assert_int_eq(result, 0x1);

CAN Signal Encoding

The library supports encoding floating point CAN signals as well as booleans into a uint64_t payload.

c
uint64_t payload = eightbyte_encode_float(1, 1, 3, 1, 0)
// payload == 0x1000000000000000

payload = eightbyte_encode_bool(true, 1, 3);
// payload == 0x1000000000000000

CAN Signal Decoding

The library supports parsing floating point CAN signals as well as booleans.

c
uint64_t payload = 0xeb00000000000000;
float float_result = eightbyte_parse_float(payload,
        2, // starting bit
        4, // width of the signal's field
        1001.0, // transformation factor for the signal value
        -30000.0); // transformation offset for the signal value
// float_result == -19990.0

bool bool_result = eightbyte_parse_bool(payload,
        0, // starting bit
        1, // width of the signal's field
        1.0, // transformation factor for the signal value
        0); // transformation offset for the signal value
// bool_result == true

Testing

The library includes a test suite that uses the check C unit test library. It requires the unit testing library check.

$ make test

You can also see the test coverage if you have lcov installed and the BROWSER environment variable set to your choice of web browsers:

$ BROWSER=google-chrome-stable make coverage

Authors

Chris Peplin [email protected]

License

Copyright (c) 2013 Ford Motor Company

Licensed under the BSD license.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言