5
9
2014
7

bitmap操作

一下代码实现了位图的基本操作

/*以addr指向位置为起始地址,第nr个bit位的操作
  内容来自于文件系统中内核源码。bitmap.h
*/


//返回1,则该bit位为1
static  int happyhut_test_bit(unsigned int nr,char * addr) 
{
   //addr为char * 类型,一个char* 为8个bit,故addr[nr>>3],使其指向该nr对应的字节
   //(1<<(nr&7))确定该字节要test的bit位
  return (addr[nr >> 3] & (1<<((nr & 7)))) != 0;
}

static  int happyhut_set_bit(unsigned int nr,char * addr)
{
  int __res = happyhut_test_bit(nr,addr);
  addr[nr >> 3] |= (1<<((nr & 7)));//按位或,置1
  return __res != 0; \
}
//置0
static  int happyhut_clear_bit(unsigned int nr,char * addr)
{
  int __res = happyhut_test_bit(nr,addr);
  addr[nr >> 3] &= ~(1<<((nr & 7)));
  return __res != 0;
}
static int happyhut_test_and_clear_bit(unsigned int nr,char * addr)
{
	int __res = happyhut_test_bit(nr,addr);
	if(__res)//返回值为1,则该bit位为1
	happyhut_clear_bit(nr,addr);
	return __res;
}
static  int happyhut_test_and_set_bit(unsigned int nr,char * addr)
{
       int __res=happyhut_test_bit(nr,addr);
	 if(!__res)
	 happyhut_set_bit(nr,addr);
	 return __res;
	 	
}
//找第一个0的bit位
static int happyhut_find_first_zero_bit(char *addr,unsigned int size)
{
 unsigned int index;
 for(index=0;index<size;index++)
	 if(!happyhut_test_bit(index,addr))
		 return index;
 return index;
}

下面看图,帮助理解。addr指向一个char,char占8个字节

 

Category: 编程理解 | Tags: | Read Count: 1745
Avatar_small
Digital Ali 说:
2021年9月07日 19:16

Really I enjoy your site with effective and useful information. It is included very nice post with a lot of our resources.thanks for share. i enjoy this post. crackstream nba

Avatar_small
Digital Ali 说:
2021年9月08日 16:06 Thank you because you have been willing to share information with us. we will always appreciate all you have done here because I know you are very concerned with our. บาคาร่า
Avatar_small
Digital Ali 说:
2021年9月08日 18:46

I am happy to find your distinguished way of writing the post. Now you make it easy for me to understand and implement the concept. Thank you for the post. 123 movies

Avatar_small
Digital Ali 说:
2021年9月09日 21:29

I have recently started a blog, the info you provide on this site has helped me greatly. Thanks for all of your time & work. https://docs.google.com/spreadsheets/d/1ntiogcbDulo3Vl9Htd7Jl9Xy-2kUp7-F33vbQ4q67Uc/edit#gid=409280497

Avatar_small
kia 说:
2021年10月11日 21:00

nice post...<a href="https://wwv.123moviesfree.world/">movies123</a>

Avatar_small
kia 说:
2021年10月11日 21:01

nice post..<a href="https://wwv.123moviesfree.world/">movies123</a>

Avatar_small
AP SSC Evs Model Pap 说:
2022年9月11日 14:42

Every Telugu Medium, English Medium and Urdu Medium student of the State Board can download the AP 10th Class EVS Model Paper 2023 Pdf with answers for term-1 & term-2 exams of SA-1, SA-2 and other exams of the board. AP SSC Evs Model Paper Environmental Education is one of the most important subjects and it’s a part of Science. School Education Department and various leading private school teaching staff have designed and suggested the practice question paper for all Part-A, Part-B, Part-C, and Part-D questions of SA-1, SA-2, FA-1, FA-2, FA-3, FA-4 and Assignments. Advised to everyone can contact the class teacher to get important questions for all lessons and topics of EVS.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com