在使用位运算符查找字符串中的重复项时出现问题。

huangapple go评论70阅读模式
英文:

Problem in finding duplicates in a string using bitwise operator

问题

I think, I have implemented right logic in C++ to find duplicates in a string using bit manipulation. but I am unable to understand why it's not printing anything in terminal. can anyone explain why this problem is occurring here ?

#include<stdio.h>

int main(){
    char A[] = "finiding
#include<stdio.h>

int main(){
    char A[] = "finiding\0";
    long int H=0;
    long int x=0;
    for(int i=0; A[i]!='\0'; i++){
        x=1;
        int temp = A[i];
        x = x<<(temp-97);
        if(x&H > 0){
            printf(" %c duplicate found !",A[i]);
        }else{
            H = x|H;
        }
    }
    return 0;
}
";
long int H=0; long int x=0; for(int i=0; A[i]!='
#include&lt;stdio.h&gt;

int main(){
    char A[] = "finiding\0";
    long int H=0;
    long int x=0;
    for(int i=0; A[i]!='\0'; i++){
        x=1;
        int temp = A[i];
        x = x<<(temp-97);
        if(x&H > 0){
            printf(" %c duplicate found !",A[i]);
        }else{
            H = x|H;
        }
    }
    return 0;
}
'
; i++){
x=1; int temp = A[i]; x = x<<(temp-97); if(x&H > 0){ printf(" %c duplicate found !",A[i]); }else{ H = x|H; } } return 0; }

I have checked the for loop is executing 8 times which is desired according to the string "finding" but control is not going into the if else block which is written into the for loop.

I have used "H" as hashing variable where I am setting the bit which is corresponding to the ASCII code of the character coming into the loop each time. and then using if else I am checking whether the bit is set or not by the concept of masking but still it's not working, and I am unable to understand why it is so.

英文:

I think, I have implemented right logic in C++ to find duplicates in a string using bit manipulation. but I am unable to understand why it's not printing anything in terminal. can anyone explain why this problem is occurring here ?

#include&lt;stdio.h&gt;

int main(){
char A[] = &quot;finiding
#include&lt;stdio.h&gt;
int main(){
char A[] = &quot;finiding\0&quot;;
long int H=0;
long int x=0;
for(int i=0; A[i]!=&#39;\0&#39;; i++){
x=1;
int temp = A[i];
x = x&lt;&lt;(temp-97);
if(x&amp;H &gt; 0){
printf(&quot; %c duplicate found !&quot;,A[i]);
}else{
H = x|H;
}
}
return 0;
}
&quot;; long int H=0; long int x=0; for(int i=0; A[i]!=&#39;
#include&lt;stdio.h&gt;
int main(){
char A[] = &quot;finiding\0&quot;;
long int H=0;
long int x=0;
for(int i=0; A[i]!=&#39;\0&#39;; i++){
x=1;
int temp = A[i];
x = x&lt;&lt;(temp-97);
if(x&amp;H &gt; 0){
printf(&quot; %c duplicate found !&quot;,A[i]);
}else{
H = x|H;
}
}
return 0;
}
&#39;; i++){ x=1; int temp = A[i]; x = x&lt;&lt;(temp-97); if(x&amp;H &gt; 0){ printf(&quot; %c duplicate found !&quot;,A[i]); }else{ H = x|H; } } return 0; }

I have checked the for loop is executing 8 times which is desired according to the string "finding" but control is not going into the if else block which is written into the for loop.

I have used "H" as hashing variable where I am setting the bit which is corresponding to the ASCII code of the character coming into the loop each time. and then using if else I am checking whether the bit is set or not by the concept of masking but still it's not working, and I am unable to understand why it is so.

答案1

得分: 1

条件x&amp;H &gt; 0被解释为x &amp; (H &gt; 0),因为&gt;运算符的优先级高于&amp;运算符。

C++ 运算符优先级 - cppreference.com

添加括号来比较AND操作的结果是否大于零,如(x&amp;H) &gt; 0

英文:

The condition x&amp;H &gt; 0 is interpreted as x &amp; (H &gt; 0) because &gt;
operator has higher precedence than &amp; operator.

C++ Operator Precedence - cppreference.com

Add parenthesis to compare the result of AND operation with zero like (x&amp;H) &gt; 0.

huangapple
  • 本文由 发表于 2023年3月20日 23:06:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75791993.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定