use criterion::{black_box, criterion_group, criterion_main, Criterion};
use num_traits::{Unsigned, Zero};
use std::any::{Any, TypeId};
use std::fmt::{format, Debug};
use std::io::{Error, ErrorKind};
use std::ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Not, Rem, Shl, Shr, Sub};
pub fn get_bit_from_u<T>(data: T, index: u8, one: T, zero: T) -> Result<bool, Error>
where
T: Unsigned
+ Copy
+ Clone
+ Debug
+ Default
+ PartialEq
+ PartialOrd
+ Add<Output = T>
+ Sub<Output = T>
+ Mul<Output = T>
+ Div<Output = T>
+ Rem<Output = T>
+ BitAnd<Output = T>
+ BitOr<Output = T>
+ BitXor<Output = T>
+ Not<Output = T>
+ Shl<u8, Output = T>
+ Shr<u8, Output = T>
+ 'static,
{
let mut max_index: u8 = 0;
match data.type_id() {
u8_type if u8_type==TypeId::of::<u8>() => {
if index > 7 {
return Err(Error::new(
ErrorKind::InvalidData,
format!("index 值超出了U8位的索引范围"),
));
}
max_index = 7;
},
u16_type if u16_type==TypeId::of::<u16>() => {
if index > 15 {
return Err(Error::new(
ErrorKind::InvalidData,
format!("index 值超出了U16位的索引范围"),
));
}
max_index = 15;
},
u32_type if u32_type==TypeId::of::<u32>() => {
if index > 31 {
return Err(Error::new(
ErrorKind::InvalidData,
format!("index 值超出了U32位的索引范围"),
));
}
max_index = 31;
},
u64_type if u64_type==TypeId::of::<u64>() => {
if index > 63 {
return Err(Error::new(
ErrorKind::InvalidData,
format!("index 值超出了U64位的索引范围"),
));
}
max_index = 63;
},
u128_type if u128_type==TypeId::of::<u128>() => {
if index > 127 {
return Err(Error::new(
ErrorKind::InvalidData,
format!("index 值超出了U128位的索引范围"),
));
}
max_index = 127;
},
_ => {
return Err(Error::new(
ErrorKind::InvalidData,
format!("输入的数据类型不是无符号整数"),
));
},
}
Ok((data >> index) & one != zero)
}
pub fn get_bit_from_u8(data: u8, index: u8) -> Result<bool, Error> {
if index > 7 {
return Err(Error::new(
ErrorKind::InvalidData,
format!("index 值超出了U8位的索引范围"),
));
}
Ok((data >> index) & 1 != 0)
}
pub fn get_bit_from_u16(data: u16, index: u8) -> Result<bool, Error> {
if index > 15 {
return Err(Error::new(
ErrorKind::InvalidData,
format!("index 值超出了U16位的索引范围"),
));
}
Ok((data >> index) & 1 != 0)
}
pub fn get_bit_from_u32(data: u32, index: u8) -> Result<bool, Error> {
if index > 31 {
return Err(Error::new(
ErrorKind::InvalidData,
format!("index 值超出了U32位的索引范围"),
));
}
Ok((data >> index) & 1 != 0)
}
pub fn get_bit_from_u64(data: u64, index: u8) -> Result<bool, Error> {
if index > 63 {
return Err(Error::new(
ErrorKind::InvalidData,
format!("index 值超出了U64位的索引范围"),
));
}
Ok((data >> index) & 1 != 0)
}
pub fn get_bits_from_u8(data: u8, index: Vec<u8>) -> Result<Vec<bool>, Error> {
if index.iter().count() > 8 {
return Err(Error::new(
ErrorKind::InvalidData,
format!("index 成员数量超过了8个"),
));
}
for &item in index.iter() {
if item > 7 {
return Err(Error::new(
ErrorKind::InvalidData,
format!("index 成员的值超过了7"),
));
}
}
let mut value = Vec::<bool>::with_capacity(8);
for i in 0..=7 {
value.push((data >> i) & 1 != 0);
}
let mut vec_bool = Vec::<bool>::new();
for &item in index.iter() {
vec_bool.push(value[item as usize]);
}
Ok(vec_bool)
}
pub fn get_bits_from_u16(data: u16, index: Vec<u8>) -> Result<Vec<bool>, Error> {
if index.iter().count() > 16 {
return Err(Error::new(
ErrorKind::InvalidData,
format!("index 成员数量超过了16个"),
));
}
for &item in index.iter() {
if item > 15 {
return Err(Error::new(
ErrorKind::InvalidData,
format!("index 成员的值超过了15"),
));
}
}
let mut value = Vec::<bool>::with_capacity(16);
for i in 0..=15 {
value.push((data >> i) & 1 != 0);
}
let mut vec_bool = Vec::<bool>::new();
for &item in index.iter() {
vec_bool.push(value[item as usize]);
}
Ok(vec_bool)
}
pub fn get_bits_from_u32(data: u32, index: Vec<u8>) -> Result<Vec<bool>, Error> {
if index.iter().count() > 32 {
return Err(Error::new(
ErrorKind::InvalidData,
format!("index 成员数量超过了32个"),
));
}
for &item in index.iter() {
if item > 31 {
return Err(Error::new(
ErrorKind::InvalidData,
format!("index 成员的值超过了31"),
));
}
}
let mut value = Vec::<bool>::with_capacity(32);
for i in 0..=31 {
value.push((data >> i) & 1 != 0);
}
let mut vec_bool = Vec::<bool>::new();
for &item in index.iter() {
vec_bool.push(value[item as usize]);
}
Ok(vec_bool)
}
pub fn get_bits_from_u64(data: u64, index: Vec<u8>) -> Result<Vec<bool>, Error> {
if index.iter().count() > 64 {
return Err(Error::new(
ErrorKind::InvalidData,
format!("index 成员数量超过了64个"),
));
}
for &item in index.iter() {
if item > 63 {
return Err(Error::new(
ErrorKind::InvalidData,
format!("index 成员的值超过了63"),
));
}
}
let mut value = Vec::<bool>::with_capacity(64);
for i in 0..=63 {
value.push((data >> i) & 1 != 0);
}
let mut vec_bool = Vec::<bool>::new();
for &item in index.iter() {
vec_bool.push(value[item as usize]);
}
Ok(vec_bool)
}
#[cfg(test)]
mod test {
use super::*;
use crate::get_bits::get_bit_from_u8;
use std::fmt::format;
use std::io::{Error, ErrorKind};
#[test]
fn test_get_bit_from_u8() {
let data1: u8 = 0b1100_1100;
assert_eq!(true, get_bit_from_u8(data1, 2).unwrap());
assert_eq!(false, get_bit_from_u8(data1, 1).unwrap());
assert_eq!(false, get_bit_from_u8(data1, 0).unwrap());
assert_eq!(true, get_bit_from_u8(data1, 7).unwrap());
assert_eq!(true, get_bit_from_u(data1, 2, 1, 0).unwrap());
assert_eq!(false, get_bit_from_u(data1, 1, 1, 0).unwrap());
assert_eq!(false, get_bit_from_u(data1, 0, 1, 0).unwrap());
assert_eq!(true, get_bit_from_u(data1, 7, 1, 0).unwrap());
assert_eq!(
Error::new(
ErrorKind::InvalidData,
format!("index 值超出了U8位的索引范围")
)
.to_string(),
get_bit_from_u8(data1, 10).unwrap_err().to_string()
);
assert_eq!(
Error::new(
ErrorKind::InvalidData,
format!("index 值超出了U8位的索引范围")
)
.to_string(),
get_bit_from_u(data1, 10, 1, 0).unwrap_err().to_string()
);
println!("错误值{:?}", get_bit_from_u8(data1, 10));
}
#[test]
fn test_get_bit_from_u16() {
let data1: u16 = 0b1100_1100_1100_0011;
assert_eq!(true, get_bit_from_u16(data1, 0).unwrap());
assert_eq!(false, get_bit_from_u16(data1, 2).unwrap());
assert_eq!(true, get_bit_from_u16(data1, 7).unwrap());
assert_eq!(true, get_bit_from_u16(data1, 15).unwrap());
assert_eq!(true, get_bit_from_u(data1, 0, 1, 0).unwrap());
assert_eq!(false, get_bit_from_u(data1, 2, 1, 0).unwrap());
assert_eq!(true, get_bit_from_u(data1, 7, 1, 0).unwrap());
assert_eq!(true, get_bit_from_u(data1, 15, 1, 0).unwrap());
assert_eq!(
Error::new(
ErrorKind::InvalidData,
format!("index 值超出了U16位的索引范围")
)
.to_string(),
get_bit_from_u16(data1, 16).unwrap_err().to_string()
);
println!("错误值{:?}", get_bit_from_u16(data1, 16));
}
#[test]
fn test_get_bit_from_u32() {
let data1: u32 = 0b1100_1100_1100_0011_1100_1100_1100_0011;
assert_eq!(true, get_bit_from_u32(data1, 0).unwrap());
assert_eq!(false, get_bit_from_u32(data1, 2).unwrap());
assert_eq!(true, get_bit_from_u32(data1, 7).unwrap());
assert_eq!(true, get_bit_from_u32(data1, 31).unwrap());
assert_eq!(
Error::new(
ErrorKind::InvalidData,
format!("index 值超出了U32位的索引范围")
)
.to_string(),
get_bit_from_u32(data1, 32).unwrap_err().to_string()
);
assert_eq!(true, get_bit_from_u(data1, 0,1,0).unwrap());
assert_eq!(false, get_bit_from_u(data1, 2,1,0).unwrap());
assert_eq!(true, get_bit_from_u(data1, 7,1,0).unwrap());
assert_eq!(true, get_bit_from_u(data1, 31,1,0).unwrap());
assert_eq!(
Error::new(
ErrorKind::InvalidData,
format!("index 值超出了U32位的索引范围")
)
.to_string(),
get_bit_from_u(data1, 32,1,0).unwrap_err().to_string()
);
println!("错误值{:?}", get_bit_from_u32(data1, 32));
}
#[test]
fn test_get_bit_from_u64() {
let data1: u64 =
0b1100_1100_1100_0011_1100_1100_1100_0011_1100_1100_1100_0011_1100_1100_1100_0011;
assert_eq!(true, get_bit_from_u64(data1, 0).unwrap());
assert_eq!(false, get_bit_from_u64(data1, 2).unwrap());
assert_eq!(true, get_bit_from_u64(data1, 7).unwrap());
assert_eq!(true, get_bit_from_u64(data1, 31).unwrap());
assert_eq!(true, get_bit_from_u64(data1, 32).unwrap());
assert_eq!(false, get_bit_from_u64(data1, 34).unwrap());
assert_eq!(true, get_bit_from_u64(data1, 39).unwrap());
assert_eq!(true, get_bit_from_u64(data1, 63).unwrap());
assert_eq!(
Error::new(
ErrorKind::InvalidData,
format!("index 值超出了U64位的索引范围")
)
.to_string(),
get_bit_from_u64(data1, 64).unwrap_err().to_string()
);
assert_eq!(true, get_bit_from_u(data1, 0,1,0).unwrap());
assert_eq!(false, get_bit_from_u(data1, 2,1,0).unwrap());
assert_eq!(true, get_bit_from_u(data1, 7,1,0).unwrap());
assert_eq!(true, get_bit_from_u(data1, 31,1,0).unwrap());
assert_eq!(true, get_bit_from_u(data1, 32,1,0).unwrap());
assert_eq!(false, get_bit_from_u(data1, 34,1,0).unwrap());
assert_eq!(true, get_bit_from_u(data1, 39,1,0).unwrap());
assert_eq!(true, get_bit_from_u(data1, 63,1,0).unwrap());
assert_eq!(
Error::new(
ErrorKind::InvalidData,
format!("index 值超出了U64位的索引范围")
)
.to_string(),
get_bit_from_u(data1, 64,1,0).unwrap_err().to_string()
);
println!("错误值{:?}", get_bit_from_u64(data1, 64));
}
#[test]
fn test_get_bits_from_u8() {
let data1: u8 = 0b1100_0011;
let index: Vec<u8> = vec![1, 3, 5, 7];
assert_eq!(
vec![true, false, false, true],
get_bits_from_u8(data1, index).unwrap()
);
let index: Vec<u8> = vec![0, 2, 4, 6];
assert_eq!(
vec![true, false, false, true],
get_bits_from_u8(data1, index).unwrap()
);
let index: Vec<u8> = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
assert_eq!(
Error::new(ErrorKind::InvalidData, format!("index 成员数量超过了8个")).to_string(),
get_bits_from_u8(data1, index).unwrap_err().to_string()
);
let index: Vec<u8> = vec![0, 1, 8];
assert_eq!(
Error::new(ErrorKind::InvalidData, format!("index 成员的值超过了7")).to_string(),
get_bits_from_u8(data1, index).unwrap_err().to_string()
);
}
#[test]
fn test_get_bits_from_u16() {
let data1: u16 = 0b1100_0011_1100_0011;
let index: Vec<u8> = vec![1, 3, 5, 7, 9, 11, 13, 15];
assert_eq!(
vec![true, false, false, true, true, false, false, true],
get_bits_from_u16(data1, index).unwrap()
);
let index: Vec<u8> = vec![0, 2, 4, 6, 8, 10, 12, 14];
assert_eq!(
vec![true, false, false, true, true, false, false, true],
get_bits_from_u16(data1, index).unwrap()
);
let index: Vec<u8> = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17];
assert_eq!(
Error::new(ErrorKind::InvalidData, format!("index 成员数量超过了16个")).to_string(),
get_bits_from_u16(data1, index).unwrap_err().to_string()
);
let index: Vec<u8> = vec![0, 1, 8, 16];
assert_eq!(
Error::new(ErrorKind::InvalidData, format!("index 成员的值超过了15")).to_string(),
get_bits_from_u16(data1, index).unwrap_err().to_string()
);
}
#[test]
fn test_get_bits_from_u32() {
let data1: u32 = 0b1100_0011_1100_0011_1100_0011_1100_0011;
let index: Vec<u8> = vec![1, 3, 5, 7, 9, 11, 13, 15];
assert_eq!(
vec![true, false, false, true, true, false, false, true],
get_bits_from_u32(data1, index).unwrap()
);
let index: Vec<u8> = vec![17, 19, 21, 23, 25, 27, 29, 31];
assert_eq!(
vec![true, false, false, true, true, false, false, true],
get_bits_from_u32(data1, index).unwrap()
);
let index: Vec<u8> = vec![0, 2, 4, 6, 8, 10, 12, 14];
assert_eq!(
vec![true, false, false, true, true, false, false, true],
get_bits_from_u32(data1, index).unwrap()
);
let index: Vec<u8> = vec![16, 18, 20, 22, 24, 26, 28, 30];
assert_eq!(
vec![true, false, false, true, true, false, false, true],
get_bits_from_u32(data1, index).unwrap()
);
let index: Vec<u8> = vec![
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31, 32,
];
assert_eq!(
Error::new(ErrorKind::InvalidData, format!("index 成员数量超过了32个")).to_string(),
get_bits_from_u32(data1, index).unwrap_err().to_string()
);
let index: Vec<u8> = vec![0, 1, 8, 32];
assert_eq!(
Error::new(ErrorKind::InvalidData, format!("index 成员的值超过了31")).to_string(),
get_bits_from_u32(data1, index).unwrap_err().to_string()
);
}
#[test]
fn test_get_bits_from_u64() {
let data1: u64 =
0b1100_0011_1100_0011_1100_0011_1100_0011_1100_0011_1100_0011_1100_0011_1100_0011;
let index: Vec<u8> = vec![1, 3, 5, 7, 9, 11, 13, 15];
assert_eq!(
vec![true, false, false, true, true, false, false, true],
get_bits_from_u64(data1, index).unwrap()
);
let index: Vec<u8> = vec![17, 19, 21, 23, 25, 27, 29, 31];
assert_eq!(
vec![true, false, false, true, true, false, false, true],
get_bits_from_u64(data1, index).unwrap()
);
let index: Vec<u8> = vec![0, 2, 4, 6, 8, 10, 12, 14];
assert_eq!(
vec![true, false, false, true, true, false, false, true],
get_bits_from_u64(data1, index).unwrap()
);
let index: Vec<u8> = vec![16, 18, 20, 22, 24, 26, 28, 30];
assert_eq!(
vec![true, false, false, true, true, false, false, true],
get_bits_from_u64(data1, index).unwrap()
);
let index: Vec<u8> = vec![33, 35, 37, 39, 41, 43, 45, 47];
assert_eq!(
vec![true, false, false, true, true, false, false, true],
get_bits_from_u64(data1, index).unwrap()
);
let index: Vec<u8> = vec![49, 51, 53, 55, 57, 59, 61, 63];
assert_eq!(
vec![true, false, false, true, true, false, false, true],
get_bits_from_u64(data1, index).unwrap()
);
let index: Vec<u8> = vec![32, 34, 36, 38, 40, 42, 44, 46];
assert_eq!(
vec![true, false, false, true, true, false, false, true],
get_bits_from_u64(data1, index).unwrap()
);
let index: Vec<u8> = vec![48, 50, 52, 54, 56, 58, 60, 62];
assert_eq!(
vec![true, false, false, true, true, false, false, true],
get_bits_from_u64(data1, index).unwrap()
);
let index: Vec<u8> = vec![
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,
];
assert_eq!(
Error::new(ErrorKind::InvalidData, format!("index 成员数量超过了64个")).to_string(),
get_bits_from_u64(data1, index).unwrap_err().to_string()
);
let index: Vec<u8> = vec![0, 1, 8, 64];
assert_eq!(
Error::new(ErrorKind::InvalidData, format!("index 成员的值超过了63")).to_string(),
get_bits_from_u64(data1, index).unwrap_err().to_string()
);
}
}