Skip to content

Commit

Permalink
Fix bug in bit hack, and I think the julycoding#84 pull request is no…
Browse files Browse the repository at this point in the history
…t good
  • Loading branch information
ultimate010 committed Jan 3, 2014
1 parent 2ab71c0 commit c64460d
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions ebook/code/c/chapter02.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,14 @@ bool contain3(char * stra,char * strb){
return false;
}
}
//bit法,只能处理大写A等价小写a
//bit法,区分大小写
bool contain4(char * stra,char * strb){
unsigned int bitA = 0,bitB = 0;
unsigned long long bitA = 0,bitB = 0; //必须用long long,64位才不会溢出
while(*stra){
bitA |= 1 << (*stra++ & 0x3f); //取8位中的后6位,a:97 A:65
bitA |= (unsigned long long)(1l << (*stra++ & 0x3f)); //取8位中的后6位,a:97:01100001b A:65:01000001b 0x3f:00111111b;
}
while(*strb){
bitB |= 1 << (*strb++ & 0x3f);
bitB |= (unsigned long long)(1l << (*strb++ & 0x3f));
}
return !((bitA ^ bitB) & bitB); //bitA ^ bitB 之后的结果,对于bitB中非0位,为1的话表示A中与B中某位不同,所以不包含
}
Expand Down

0 comments on commit c64460d

Please sign in to comment.