mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
bitfield: fix bit order
This commit is contained in:
parent
20d6492775
commit
94b36250a1
@ -13,7 +13,7 @@ Bit arrays are stored in data structures called 'BitField'. The structure is
|
|||||||
provides API (functions and methods) for accessing and modifying bit arrays.
|
provides API (functions and methods) for accessing and modifying bit arrays.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
struct BitField {
|
pub struct BitField {
|
||||||
mut:
|
mut:
|
||||||
size int
|
size int
|
||||||
//field *u32
|
//field *u32
|
||||||
@ -77,36 +77,19 @@ fn cleartail(instance mut BitField) {
|
|||||||
|
|
||||||
// public functions
|
// public functions
|
||||||
|
|
||||||
// from_bytes() converts a byte arry into a bitfield.
|
// from_bytes() converts a byte array into a bitfield.
|
||||||
// Be aware of possible trailing zeroes being added
|
|
||||||
// due to the underlying 32bit int containers.
|
|
||||||
pub fn from_bytes(input []byte) BitField {
|
pub fn from_bytes(input []byte) BitField {
|
||||||
mut output := new(input.len * 8)
|
mut output := new(input.len * 8)
|
||||||
for i, b in input {
|
for i, b in input {
|
||||||
pos := i / 4
|
output.field[i / 4] |= u32(b) << ((i % 4) * 8)
|
||||||
match i % 4 {
|
|
||||||
0 {
|
|
||||||
output.field[pos] = output.field[pos] | (u32(b) << 24)
|
|
||||||
}
|
|
||||||
1 {
|
|
||||||
output.field[pos] = output.field[pos] | (u32(b) << 16)
|
|
||||||
}
|
|
||||||
2 {
|
|
||||||
output.field[pos] = output.field[pos] | (u32(b) << 8)
|
|
||||||
}
|
|
||||||
3 {
|
|
||||||
output.field[pos] = output.field[pos] | u32(b)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return output
|
return output
|
||||||
}
|
}
|
||||||
|
|
||||||
// str2bf() converts a string of characters ('0' and '1') to a bit
|
// from_string() converts a string of characters ('0' and '1') to a bit
|
||||||
// array. Any character different from '0' is treated as '1'.
|
// array. Any character different from '0' is treated as '1'.
|
||||||
|
|
||||||
pub fn str2bf(input string) BitField {
|
pub fn from_string(input string) BitField {
|
||||||
mut output := new(input.len)
|
mut output := new(input.len)
|
||||||
for i := 0; i < input.len; i++ {
|
for i := 0; i < input.len; i++ {
|
||||||
if input[i] != 48 {
|
if input[i] != 48 {
|
||||||
|
@ -127,16 +127,14 @@ fn test_bf_from_bytes() {
|
|||||||
output := bitfield.from_bytes(input)
|
output := bitfield.from_bytes(input)
|
||||||
mut result := 1
|
mut result := 1
|
||||||
for i := 0; i < input.len * 8; i++ {
|
for i := 0; i < input.len * 8; i++ {
|
||||||
expected := input[input.len - 1 - i / 8] >> i % 8 & 1
|
if (input[i / 8] >> (i % 8)) & 1 != output.getbit(i) {
|
||||||
actual := output.getbit(i)
|
|
||||||
if expected != actual {
|
|
||||||
result = 0
|
result = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assert result == 1
|
assert result == 1
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_bf_str2bf() {
|
fn test_bf_from_string() {
|
||||||
rand.seed(time.now().uni)
|
rand.seed(time.now().uni)
|
||||||
len := 80
|
len := 80
|
||||||
mut input := ''
|
mut input := ''
|
||||||
@ -148,7 +146,7 @@ fn test_bf_str2bf() {
|
|||||||
input = input + '0'
|
input = input + '0'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
output := bitfield.str2bf(input)
|
output := bitfield.from_string(input)
|
||||||
mut result := 1
|
mut result := 1
|
||||||
for i := 0; i < len; i++ {
|
for i := 0; i < len; i++ {
|
||||||
if input[i] != output.getbit(i) + 48 {
|
if input[i] != output.getbit(i) + 48 {
|
||||||
|
Loading…
Reference in New Issue
Block a user