Hi,
this is probably trivial but I am finding it difficult to figure it out on my own. Given two u8 vectors, what would be the most efficient way to convert them into bitstreams, xor them and convert them back to vec?
thnx
Hi,
this is probably trivial but I am finding it difficult to figure it out on my own. Given two u8 vectors, what would be the most efficient way to convert them into bitstreams, xor them and convert them back to vec?
thnx
This seems to be easily done with iterators:
let v1: Vec<u8> = vec![0, 1, 2, 3];
let v2: Vec<u8> = vec![5, 6, 7, 8];
let v3: Vec<u8> = v1
.iter()
.zip(v2.iter())
.map(|(&x1, &x2)| x1 ^ x2)
.collect();
assert_eq!(v3, vec![0 ^ 5, 1 ^ 6, 2 ^ 7, 3 ^ 8]);
If you don't need to keep the originals around, you can write the result to one of the vectors instead:
fn main() {
let mut v1: Vec<u8> = vec![0, 1, 2, 3];
let v2: Vec<u8> = vec![5, 6, 7, 8];
v1.iter_mut()
.zip(v2.iter())
.for_each(|(x1, x2)| *x1 ^= *x2);
assert_eq!(v1, vec![0 ^ 5, 1 ^ 6, 2 ^ 7, 3 ^ 8]);
}
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.