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"
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"
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:
If not, then you need to explain what is this "bitwise logic" and why do you want to use it to "compare char *".Code:const char *a = "car"; const char *b = "carb"; if (strcmp(a, b) == 0) { puts("yes"); } else { puts("no"); }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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
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.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; }
(Probably not the best of answers there could be, but I contributed to this thread too hoping to learn).
Last edited by ghoul; 04-11-2022 at 11:13 AM.
ghoul's strcmp implementation is doubly wrong!
Firstly, due to a (presumably) missing semicolon, it's actually this:
Presumably he meant this:Code:while (*a++ == *b++) return *a - *b; // missing return statement if the while loop fails the first test
But this is still wrong.Code:while (*a++ == *b++) ; return *a - *b;
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.
A little inaccuracy saves tons of explanation. - H.H. Munro
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.
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?
A little inaccuracy saves tons of explanation. - H.H. Munro
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.
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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
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;.
Note the conversion to unsigned char (if they weren't already unsigned) for the subtraction.Code:int my_strcmp(const char *a, const char *b) { while (*a && *a == *b) ++a, ++b; return (unsigned char)*a - (unsigned char)*b; }
A little inaccuracy saves tons of explanation. - H.H. Munro