# Thread: String comparison using bitwise logic?

1. ## String comparison using bitwise logic?

How do you use bitwise logic to compare char *?

char *a = "car";
char *b = "carb";
char *c = "";

//bitwise logic
//if a equals b print "yes" 2. It sounds like you just want to do something like what strcmp does to produce a yes/no result concerning the question of whether the strings are equal. If so, then it could be as simple as:
Code:
```const char *a = "car";
const char *b = "carb";
if (strcmp(a, b) == 0)
{
puts("yes");
}
else
{
puts("no");
}```
If not, then you need to explain what is this "bitwise logic" and why do you want to use it to "compare char *". 3. ## Why?

I just don't want to use any built-in functions. Also, want to learn something.

Otherwise, I can just use if (a == b). 4. Originally Posted by tibegato I just don't want to use any built-in functions. Also, want to learn something.

Otherwise, I can just use if (a == b).
No, that way you would be comparing pointers. A pointer is equal to the other point if they point to the same memory location.

Ha! I quickly put together an implementation of strcmp that even I do not have a very clear idea of how it works o.O

Code:
```include <stdio.h>

int strcmp(const char *a, const char *b)
{
while(*a++ == *b++)

return *a - *b;
}

int main(int argc, char *argv)
{
char *first = "Carb";
char *second = "Carb";

if (strcmp(first, second) == 0)
{
printf("Equal\n");
}else
{
printf("Not equal\n");
}

return 0;
}```
I'm honestly not sure how the the while condition gets evaluated such that the loop comes to an end at the end of the string(s). Butt, it seems to work.

(Probably not the best of answers there could be, but I contributed to this thread too hoping to learn). 5. ghoul's strcmp implementation is doubly wrong!
Firstly, due to a (presumably) missing semicolon, it's actually this:
Code:
```    while (*a++ == *b++)
return *a - *b;
// missing return statement if the while loop fails the first test```
Presumably he meant this:
Code:
```    while (*a++ == *b++) ;
return *a - *b;```
But this is still wrong.

What happens if *a != *b in the while test? The loop exits but the *a - *b is showing the difference between the NEXT bytes, not the ones that stopped the while loop. If those bytes just happen to be equal, it will return 0, indicating that the strings are equal.

If the strings are equal then the test will run off the end after comparing *a == *b for when they are both the terminating '\0'. then the *a - *b will subtract the two bytes after the '\0' even though those are not part of the strings!

@tibegato, comparing the strings "bitwise", although doable, would be totally retarded. 6. ## Ok

First off thank you, for your corrections on the code sample. You are correct.

Second, no one asked if comparing strings using bitwise operators was good or bad. Just want to know how. Presenting the code to do it and appending a note saying that it's really inefficient or something would've been a better approach. Originally Posted by john.c ghoul's strcmp implementation is doubly wrong!
Firstly, due to a (presumably) missing semicolon, it's actually this:
Code:
```    while (*a++ == *b++)
return *a - *b;
// missing return statement if the while loop fails the first test```
Presumably he meant this:
Code:
```    while (*a++ == *b++) ;
return *a - *b;```
But this is still wrong.

What happens if *a != *b in the while test? The loop exits but the *a - *b is showing the difference between the NEXT bytes, not the ones that stopped the while loop. If those bytes just happen to be equal, it will return 0, indicating that the strings are equal.

If the strings are equal then the test will run off the end after comparing *a == *b for when they are both the terminating '\0'. then the *a - *b will subtract the two bytes after the '\0' even though those are not part of the strings!

@tibegato, comparing the strings "bitwise", although doable, would be totally retarded. 7. It's beyond inefficient. It's literally retarded. That's why I used the technical term. Why should I waste my time writing ridiculously simple and pointless code for you? 8. ## Cause

I asked ... for learning purposes.

There's millions of things we learn, we don't actually use. I saw a post were you could toggle case of a string with a bitwise operation:

https://www.geeksforgeeks.org/toggle...ise-operators/

Would you ever do that ... probably not. Originally Posted by john.c It's beyond inefficient. It's literally retarded. That's why I used the technical term. Why should I waste my time writing ridiculously simple and pointless code for you? 9. Perhaps you could use the fact that xoring a number with itself is zero.

So comparing the first char of each string would be
if ( (*a ^ *b) == 0 )

while loops and stopping at \0 are as per usual for the task. 10. I suggest that you learn to use the standard library and perhaps other common utility libraries before you go along the path of the obscure. After you learn to use them, learn to implement them.

I mean, you didn't even know that strcmp is not the same as directing comparing pointers to char with the == operator. Have you ever implemented strcmp or something like it? If you have not yet acquired such foundational knowledge, why attempt "comparing strings using bitwise operators"? Learn the basics, then doing the esoteric will be much easier. 11. Perhaps it was a homework assignment from an unqualified instructor, a bad book, painfully inadequate online tutorial, YouTube video, ... If so, it should never been assigned, or listed. 12. Speaking of which, may be I'm hijacking this thread at this point.

My finer implementation works but chokes on NULL....

Code:
```int strCmp(const char *s1, const char *s2 )
{
const char *p1 = s1;
const char *p2 = s2;

while (*p1 == *p2)
{
if (*p1 == '\0')
return *p1 - *p2;
p1++;
p2++;
}

return *p1 - *p2;
}``` 13. Originally Posted by ghoul Speaking of which, may be I'm hijacking this thread at this point.

My finer implementation works but chokes on NULL....

Code:
```int strCmp(const char *s1, const char *s2 )
{
const char *p1 = s1;
const char *p2 = s2;

while (*p1 == *p2)
{
if (*p1 == '\0')
return *p1 - *p2;
p1++;
p2++;
}

return *p1 - *p2;
}```
It's fine if it chokes on NULL. The standard strcmp requires non-null arguments and is not guaranteed to work if one or both arguments are null (in fact, the behavior is undefined in those cases).

Also, the first return *p1 - *p2; line could be changed to return 0; (since we know the characters are the same at that point) or to break;. 14. Code:
```int my_strcmp(const char *a, const char *b)
{
while (*a && *a == *b) ++a, ++b;
return (unsigned char)*a - (unsigned char)*b;
}```
Note the conversion to unsigned char (if they weren't already unsigned) for the subtraction. Popular pages Recent additions //bitwise, //if, bitwise, char, logic 