비트필드를 배열로 만들수는 없나요??

philossh의 이미지

예를들자면..

struct BitField{
    int bf[32] : 1;
};

이런식으로.. 1비트짜리 32개의 배열을 만들 수는 없나요??
제가 해보니.. 비트필드는 indirection을 가질 수 없다고 나오는데..
그렇다면

struct BitField{
    int bf1 : 1;
    int bf2 : 1;
    ...
    ..    
    .
    int bf32 : 1;
}

이런식으로 전부 만들어 줘야 하나요??
htna의 이미지

함 bitfield 를 template class로 짜 봤습니다.
해보면 재밌을거 같아서..
^^

template<int SIZE, typename T=int>
class CBitfield
{
	enum {
		TSIZE = sizeof(T)*8,
		MEMSIZE = sizeof(T*)*SIZE,
	};
	T _value[SIZE];
	T* _Base(unsigned int index) { return &(_value[index/TSIZE]); }
	T _Offset(unsigned int index) { return 0x01<<(index%TSIZE); }
public:
	enum {
		MAXINDEX = sizeof(T)*SIZE*8
	};
	class CAccessor {
		friend CBitfield;
		T* _base;
		T _offset;
		CAccessor(T* base, T offset) : _base(base), _offset(offset) { }
		bool _bool() const {
			return ((*_base & _offset) != 0);
		}
		void _assign(bool value) {
			*_base = (value) ? (*_base | _offset) : (*_base & ~_offset);
		}
	public:
		operator bool() const {
			return _bool();
		}
		bool operator = (bool value) {
			_assign(value);
			return value;
		}
		bool operator &= (bool value) {
			_assign(value & _bool());
			return _bool();
		}
		bool operator |= (bool value) {
			_assign(value | _bool());
			return _bool();
		}
		bool operator ^= (bool value) {
			_assign(value ^ _bool());
			return _bool();
		}
	};

	CBitfield() {
		memset(_value, 0, MEMSIZE);
	}
	CBitfield(const CBitfield& value) {
		memcpy(_value, value._value, MEMSIZE);
	} 
	~CBitfield() { }

	CAccessor operator()(unsigned int index) {
		assert(index < MAXINDEX);
		return CAccessor(_Base(index),_Offset(index));
	}
	const CAccessor operator()(unsigned int index) const {
		assert(index < MAXINDEX);
		return CAccessor(_Base(index),_Offset(index));
	}
	const CBitfield& operator=(const CBitfield& value) {
		memcpy(_value, value._value, MEMSIZE);
		return *this;
	}
};

usage:

CBitfield<2> bf2;
bf2(0) = true;
bool b = bf2(0);
bf2(0) &= true;

WOW Wow!!!
Computer Science is no more about computers than astronomy is about telescopes.
-- E. W. Dijkstra

cinsk의 이미지

원 주제와는 거리가 있지만, bitmap 관련해서 검색해보면 원하시는 비슷한 기능을 얻을 수 있을 것 같습니다. 예전에 대충 만든 코드가 있습니다:

http://wiki.kldp.org/wiki.php/xbitmap

doldori의 이미지

또는 bitset이나 vector<bool>을 쓰셔도...

lacovnk의 이미지

vector<bool> 보기를 돌같이 하자

라고 이펙티브 STL 18번에 되어있군요;

첫째는 STL컨테이너가 아니다
둘째는 bool을 담고 있지도 않다..

라고 되어있네요; (잘 모릅니다! 본 적이 있어서 남겨놓습니다 ㅎ)

doldori의 이미지

lacovnk wrote:
첫째는 STL컨테이너가 아니다

네, 맞습니다. vector<bool>은 컨테이너의 요구 조건을 만족하지 않습니다.
lacovnk wrote:
둘째는 bool을 담고 있지도 않다..

요것도 맞습니다. :-)
그래도 OP께서 원하시는 구조체를 대신하기에는 부족함이 없지요.
htna의 이미지

bitset 은 목적에 맞을 수 있습니다.
하지만 vector<bool> 은 좀 메모리 낭비라고 보여지네요...
저기 위에 만든건, 단지 재미를 위해 만들어본 것입니다.
보니깐 bitset이 더 낫네요...
^^;;

PS:
불과 약 30분 정도 사이에..
엄청난 리플들이...
^^

WOW Wow!!!
Computer Science is no more about computers than astronomy is about telescopes.
-- E. W. Dijkstra

doldori의 이미지

htna wrote:
하지만 vector<bool> 은 좀 메모리 낭비라고 보여지네요...

그렇지는 않습니다. 표준은 bool에 대해 특화된(specialized) vector<bool>
클래스를 명시하고 있습니다. 이 클래스의 목적은 메모리 사용량을 최적화하기
위한 것이고요. htna님의 코드와 비슷한 방식으로 proxy class를 사용합니다.
CBitField::CAccessor와 같은 역할을 하는 것이 vector<bool>::reference이죠.
ㅡ,.ㅡ;;의 이미지

bit[32] => char[4]

로쓰면될텐데 문제가 있나요..

연산이 귀찮다면 매크로 사용해도 되고..


----------------------------------------------------------------------------

익명 사용자의 이미지

음. 이제야 좀 기억이 나네요.. 예전에 처음에 STL 볼때의 기억이...
하지만 std::vector<bool> class를 따로 정의할 수야 있겠지만, 이렇다면, 거의 기억하고 있지 않은한 사용하기 힘들겠네요.. 근데 왜 MSDN의 std::vector<T> 설명에서 못봤지...
아, 그렇다면 bitset 과 std::vector<bool> 과의 차이가 뭐죠?
bitset도 비슷한 역활을 하는 듯 한데요...

doldori wrote:
htna wrote:
하지만 vector<bool> 은 좀 메모리 낭비라고 보여지네요...

그렇지는 않습니다. 표준은 bool에 대해 특화된(specialized) vector<bool>
클래스를 명시하고 있습니다. 이 클래스의 목적은 메모리 사용량을 최적화하기
위한 것이고요. htna님의 코드와 비슷한 방식으로 proxy class를 사용합니다.
CBitField::CAccessor와 같은 역할을 하는 것이 vector<bool>::reference이죠.
htna의 이미지

로그인을 하지 않고 글을쓰니 "손님"으로 등록이 되네요...
글을 지우고 다시 쓰려고 해도 그렇게 할 수가 없구요..
로그인을 안하다니... 이궁 실수네... ^^;;;
바로 위의글 제가 쓴 글입니다.
그럼...

WOW Wow!!!
Computer Science is no more about computers than astronomy is about telescopes.
-- E. W. Dijkstra

doldori의 이미지

Anonymous wrote:
아, 그렇다면 bitset 과 std::vector<bool> 과의 차이가 뭐죠?
bitset도 비슷한 역활을 하는 듯 한데요...

가장 큰 차이점은 bitset의 길이는 고정인 반면 vector<bool>은 가변이라는 점입니다.
vector<bool>은 이름에서 알 수 있듯이 원소의 삽입, 삭제, iteration 등 vector의
특성을 많이 갖고 있습니다. 반면 bitset은 이런 조작이 불가능한 대신에 비트 연산이
편리하도록 되어 있고요. OP의 구조체 대신이라면 bitset이 더 어울리는 것 같긴 하네요.
htna의 이미지

^^;;;
bitset과 비슷한 CBitfield를 만들고도,
std::vector<T>가 뭔지 알면서도 물어본 질문이네요..
대답을 보고 잠시 멍~ 했다는.. 왜 그랬지... ㅡ.ㅜ

doldori wrote:
Anonymous wrote:
아, 그렇다면 bitset 과 std::vector<bool> 과의 차이가 뭐죠?
bitset도 비슷한 역활을 하는 듯 한데요...

가장 큰 차이점은 bitset의 길이는 고정인 반면 vector<bool>은 가변이라는 점입니다.
vector<bool>은 이름에서 알 수 있듯이 원소의 삽입, 삭제, iteration 등 vector의
특성을 많이 갖고 있습니다. 반면 bitset은 이런 조작이 불가능한 대신에 비트 연산이
편리하도록 되어 있고요. OP의 구조체 대신이라면 bitset이 더 어울리는 것 같긴 하네요.

WOW Wow!!!
Computer Science is no more about computers than astronomy is about telescopes.
-- E. W. Dijkstra

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.