Skip to content

Latest commit

 

History

18 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bitsget (Simple BitSet)

부호 없는 정수형(u8, u16, u32, u64, u128) 및 컨테이너([T; N], &[T], &mut [T], Vec<T>)를 불리언 배열처럼 간편하게 다룰 수 있게 해주는 경량 비트 조작 라이브러리입니다.

u8 하나를 [bool; 8]처럼 사용할 수 있습니다! 기본적으로 #![no_std]를 지원합니다.


주요 기능

  • 정수 타입 지원: u8, u16, u32, u64, u128
  • 컨테이너 지원: 고정 크기 배열 [T; N], 슬라이스 &[T], &mut [T], 동적 배열 Vec<T>
  • 핵심 트레이트:
    • BitSet: 비트 값 설정 (true/false)
    • BitGet: 비트 값 확인 (bool 반환)
    • BitTogle: 비트 값 반전 (토글)
  • no_std 호환: 힙 할당 없이 임베디드 및 제한된 환경에서도 사용 가능

비트 인덱싱 순서 (MSB -> LSB)

인덱스 0은 최상위 비트(MSB, 왼쪽 끝)부터 시작합니다.

u8 기준 비트 인덱스:
 Index:    0   1   2   3   4   5   6   7
 Bit:    [ 0   0   0   1   0   0   0   0 ]
           ^                           ^
          MSB                         LSB
      (0b1000_0000)               (0b0000_0001)

배열/슬라이스/Vec의 경우:

  • 요소 위치: index / (원소 타입의 비트 수)
  • 해당 요소 내 비트 오프셋: index % (원소 타입의 비트 수)

사용법

1. 단일 정수 (u8, u16, u32, u64, u128)

use bitsget::{BitGet, BitSet, BitTogle};

let mut bit = 0_u8;

// 3번 인덱스 비트를 1(true)로 설정 (0b0001_0000)
bit.bitset(3, true);
assert_eq!(bit, 0b0001_0000);
assert_eq!(bit.bitget(3), true);

// 3번 비트 토글 (1 -> 0)
bit.bittogle(3);
assert_eq!(bit.bitget(3), false);

// 3번 비트를 다시 0(false)으로 설정
bit.bitset(3, false);
assert_eq!(bit.bitget(3), false);

2. 고정 크기 배열 ([T; N])

use bitsget::{BitGet, BitSet, BitTogle};

// u8 3개 = 총 24비트
let mut bit = [0_u8; 3];

bit.bitset(20, true);
assert_eq!(bit.bitget(20), true);

bit.bittogle(20);
assert_eq!(bit.bitget(20), false);

3. 동적 배열 (Vec<T>)

use bitsget::{BitGet, BitSet, BitTogle};

let mut bit: Vec<u8> = vec![0_u8; 3];

bit.bitset(20, true);
assert_eq!(bit.bitget(20), true);

bit.bitset(20, false);
assert_eq!(bit.bitget(20), false);

문서 생성

로컬에서 브라우저를 통해 상세한 API 문서와 주석을 확인하려면 아래 명령어를 실행하세요:

cargo doc --open

About

No description or website provided.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages