`

bit_array - simple use

 
阅读更多

bit array,use less memory to describe a set of int number,

 

example:

/**
 * use bit string to store int numbers,if char[n] = 1,then n is include,else n is not exclude,
 */

#include <stdio.h>

// set a bit to 1,which indicate that a number is include,
void setbit(char data[], int pos);
// get a bit,1 = include,0 = exclulde,
char getbit(char data[], int pos);
// indicate the position of bit in byte[]
struct bit_of_ints {
	int a; // byte's subscript
	char b; // position in a single byte,0 - 7,
};
// get position struct by pos,
struct bit_of_ints figure_bit_of_ints(int pos);

int main() {
	int length = 100;
	// figure byte count
	int bytecount = (length >> 3) + (length % 8 == 0 ? 0 : 1);
	char data[bytecount];
	int i;
	// init byte[]
	for (i = 0; i < bytecount; i++) {
		data[i] = 0;
	}
	// set bits
	for (i = 0; i < length; i++) {
		if (i % 10 == 0) {
			setbit(data, i);
		}
	}
	// get bits
	for (i = 0; i < length; i++) {
		if (getbit(data, i) != 0) {
			printf("%d,", i);
		}
	}
	printf("\n");
	return 0;
}

struct bit_of_ints figure_bit_of_ints(int pos) {
	struct bit_of_ints boi;
	char b = pos % 8;
	boi.a = pos >> 3;
	switch (b) {
	case 0:
		boi.b = 0x80;
		break;
	case 1:
		boi.b = 0x40;
		break;
	case 2:
		boi.b = 0x20;
		break;
	case 3:
		boi.b = 0x10;
		break;
	case 4:
		boi.b = 0x08;
		break;
	case 5:
		boi.b = 0x04;
		break;
	case 6:
		boi.b = 0x02;
		break;
	case 7:
		boi.b = 0x01;
		break;
	default:
		break;
	}
	return boi;
}

void setbit(char data[], int pos) {
	struct bit_of_ints bis = figure_bit_of_ints(pos);
	data[bis.a] |= bis.b;
}

char getbit(char data[], int pos) {
	struct bit_of_ints bis = figure_bit_of_ints(pos);
	return data[bis.a] & bis.b;
}

分享到:
评论

相关推荐

    r40_tinav2.1_最终验证通过_使用CB-S来验证SPI2.0成功_20171114_0945没有外层目录.7z

    # The defaults are appropriate for compiling a simple program such as this one # Specify where and how to install the program. Since we only have one file, # the helloworld executable, install it by...

    Git-2.21.0-64-bit.zip

    of the array in use. This has been corrected. * The description about slashes in gitignore patterns (used to indicate things like "anchored to this level only" and "only matches directories") has...

    Bochs - The cross platform IA-32 (x86) emulator

    [3043174] Docbook use of '_' build failure [3085140] Ia_arpl_Ew_Rw definition of error [3078995] ROL/ROR/SHL/SHR modeling wrong when dest reg is 32 bit [2864794] BX_INSTR_OPCODE in "cpu_loop" ...

    jna-4.2.2 官方原版下载

    Java array and NIO Buffer arguments (primitive types and pointers) as pointer-to-buffer Nested structures and arrays Wide (wchar_t-based) strings Native long support (32- or 64-bit as appropriate)...

    Program.Arcade.Games.With.Python.and.Pygame.14842178

    Learn and use Python and PyGame to design and build cool arcade games. In Program Arcade Games: With Python and PyGame, Second Edition, Dr. Paul Vincent Craven teaches you how to create fun and simple...

    The Art of Assembly Language Programming

    Pointers to Structures 5.7 - Sample Programs 5.7.1 - Simple Variable Declarations 5.7.2 - Using Pointer Variables 5.7.3 - Single Dimension Array Access 5.7.4 - Multidimensional Array ...

    delphi编译错误.txt

    7. **16-Bit fixup encountered in object file `&lt;text&gt;`** - **含义**: 在对象文件 `&lt;text&gt;` 中遇到了 16 位修复。 - **解决办法**: 确保所有使用的库都是 32 位版本。 8. **486/487 instructions not enabled*...

    libtomcrypt-1.17

    LibTomCrypt has been designed from the ground up to be very simple to use. It has a modular and standard API that allows new ciphers, hashes and PRNGs to be added or removed without change to the ...

    javacv-platform-1.3.3-src

    Finally, please make sure everything has the same bitness: 32-bit and 64-bit modules do not mix under any circumstances. Manual Installation Simply put all the desired JAR files (opencv*.jar, ffmpeg...

    SQLMemTable for Delphi / C++ Builder

    All text search and sorting functions use current system locale, so localizing your program with SQLMemTable is a very simple task. - Unicode support. All the text operations work with multi-byte ...

    编程珠玑全部源代码 分享

    bitsort.c -- Sort with bit vectors. sortints.cpp -- Sort using C++ STL sets. qsortints.c -- Sort with C library qsort. bitsortgen.c -- Generate random integers for sorting. Column 2: Test and ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Length Arrays and alloca() Friends Exceptions Run-Time Type Information (RTTI) Casting Streams Preincrement and Predecrement Use of const Integer Types 64-bit Portability Preprocessor Macros 0 and ...

    RxLib控件包内含RxGIF,全部源码及DEMO

    TRxTrayIcon component enables 32-bit applications to add static and animated icons to the Windows system tray. TRxClock, TSecretPanel, TRxDice, TRxCalculator, TStrHolder, TMRUManager, TRxWindowHook, ...

    Practical C++ Programming C++编程实践

    Modular Programming Modules Public and Private The extern Storage Class Headers The Body of the Module A Program to Use Infinite Arrays The Makefile for Multiple Files Using the Infinite Array ...

    Visual C++ 编程资源大全(英文源码 DLL)

    lookaside_src.zip A simple way to keep items such as COM instances 'warm' and available for reuse (3KB)&lt;END&gt;&lt;br&gt;57,isarray_src.zip A simple templated array class. (2KB)&lt;END&gt;&lt;br&gt;58,...

    BlitzMax v1.30

    Array and string slicing Flexible 'collection' system for dealing with linked lists etc Low level pointer handling UTF16 strings The ability to 'Incbin' binary data and access it as easily as if ...

    CE中文版-启点CE过NP中文.exe

    When using ranges, do not use interrupts, or use DBVM Added find what writes/access to the foundlist Autoassembler scriptblocks are now grouped when written to memory Added {$try}/{$except} to auto ...

    BobBuilder_app

    These tests were done on a HP ML120G6 system with 12Gb Ram, 10k raid disk drives running Windows 2008 Server R2 64 bit. For a measure of relative performance to RaptorDb v1 I have included a 20 ...

    2009 达内Unix学习笔记

    集合了 所有的 Unix命令大全 ...telnet 192.168.0.23 自己帐号 sd08077-you0 ftp工具 192.168.0.202 tools-toolss ... 各个 shell 可互相切换 ksh:$ sh:$ csh:guangzhou% bash:bash-3.00$ ... 命令和参数之间必需用空格隔...

    C PROGRAMMING TUTORIAL

    Perform bit-level operations. - **`&`:** Bitwise AND. - **`|`:** Bitwise OR. - **`^`:** Bitwise XOR. - **`~`:** Bitwise NOT. - **`** Left shift. - **`&gt;&gt;`:** Right shift. ##### Assignment Operators ...

Global site tag (gtag.js) - Google Analytics