`"0111"`

as the pattern it reads that as a decimal number which is `0110 1111`

in binary. You need to read it as a string and build the bit mask from it: 1 character in the string equals one bit in the mask.
I am writing a code that converts the number entered into a binary number without repetitive and array sentences and compares it with the pattern I entered. I wonder if this can be done using only bit operators

**What I have tried:**

C++

#include <stio.h> int main(void) { int num; int pattern; char answer; printf("Enter a number from 1 to 255: "); scanf_s("%d", num); printf("Enter a a four-digit pattern you want to find (ex: 0100): "); scanf_s("%d", pattern); /* Only use bitwise operator, i have to compare 'num' and 'pattern' ex) num = 15(00001111) pattern = 0111 output : pattern 0111 found! without conditional statements or arrangements This is what I'm curious about.*/ answer = (/* here too*/) ? "found!" : "not found"; printf("pattern %d %c", pattern, answer); return 0; }

Comments

You need to convert the pattern into its correct representation. What you are doing is converting it to a decimal number. If you enter

`"0111"`

as the pattern it reads that as a decimal number which is `0110 1111`

in binary. You need to read it as a string and build the bit mask from it: 1 character in the string equals one bit in the mask.
Permalink

Share this answer

Comments

CPallini
4-Oct-22 5:25am

5.

Richard MacCutchan
4-Oct-22 5:58am

:thumbsup:

You read the two numbers in the variables "num" and "pattern" in different place value systems both as decimal numbers.

If you want to compare both, both must be in the same place value system. The two place value systems work according to these patterns:

for "pattern":

for "num":

While for "num" each bit already has its "correct" value, for "pattern" the "correct" value would have to be calculated first. Since the result is to be represented as a dual number and the "correct" value is not required here, it would be more favorable to convert "num" into the same system in which "pattern" already exists.

Then you can compare both and output them decimal with up to 8 leading zeros.

Converting "num" to the desired format is trivially easy, but it is probably a homework that I will not do here.

PS: The suggestion to make strings out of both numbers and compare them would be possible, but much more time consuming, because you would have to convert both numbers and then compare strings.

Finally, one more thing: Since "answer" is obviously a string, it cannot be output with %c.

If you want to compare both, both must be in the same place value system. The two place value systems work according to these patterns:

for "pattern":

0001 = 1 0010 = 2 0100 = 4 1000 = 8

for "num":

1 = 1 2 = 2 4 = 4 8 = 8

While for "num" each bit already has its "correct" value, for "pattern" the "correct" value would have to be calculated first. Since the result is to be represented as a dual number and the "correct" value is not required here, it would be more favorable to convert "num" into the same system in which "pattern" already exists.

Then you can compare both and output them decimal with up to 8 leading zeros.

Converting "num" to the desired format is trivially easy, but it is probably a homework that I will not do here.

PS: The suggestion to make strings out of both numbers and compare them would be possible, but much more time consuming, because you would have to convert both numbers and then compare strings.

Finally, one more thing: Since "answer" is obviously a string, it cannot be output with %c.

Permalink

Share this answer

Comments

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject,
20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8
+1 (416) 849-8900

bitpattern" Also why a fixed bit pattern length? Any number of binary digits [1, 8] seems reasonable. Congratulations Mr. MacCutchan for doing homework for poster. Perhaps I am mistaken but a simpler algorithm may exist i.e. to wit allow the STL to do all the work in perhaps two statements.