Comparing hex numbers

Hello, everyone. I have a question. I have a task in which I have to cin a number (not bigger than 2^32) and the program have to check if it's numbers in hex are equal. I can't use loops. So with the help of mask, I separate the numbers but now I don't know how exactly to compare them all, especially when not all 8 avaliable positions are not occupied... (ex. 1365 in hex is equal to 555, but the number 555 doesn't have 8 digits, so the first 5 are zeros... ). Here is my code:

Why not use loops? Is there something you haven't told the forum such as this is a homework assignment or a code challenge?

Programmers rarely limit themselves like this. Also, your software specification is unclear here. But that won't stop you. Think over how you would do this manually. Now sketch out those steps on paper, and then implement each step in code.

Why not use loops? Is there something you haven't told the forum such as this is a homework assignment or a code challenge?

Has to be. Lots of teachers intentionally start off by making students do things a ridiculously difficult way by adding needless constraints like "no loops". Then next week's assignment is to re-write it with a loop. This will supposedly make you appreciate loops more than if you just used a loop the first time. Thus I suppose our job as mentors for lack of a better term is to guide the student in how to do it the wrong way. Something like that.

That said, even without loops, it seems like you have too many variables. Along the lines of rproffitt's suggestion, how bout this for an algorithm...

Start with number x with some value: AAAA

Split x into two separate parts: the last hex digit and everything but that: AAA|A

Stick the AAA into the variable x and have another variable called digit, so x = AAA, digit = A.

If x is 0, we're done. All the hex digits match. In this case, x is AAA, not 0, so we continue.

Now again split x into two pieces: AA|A. Stick that last digit in a variable called lastDigit. The remainder is stored in x. So now x is AA and lastDigit is A and digit is A.

Compare digit and lastDigit. If they are different, then you're done: at least one hex digit does not match.

If they match, repeat step 4.

4 to 7 is a loop with two possible exits to the loop: x == 0 or digit != lastDigit. Since you can't use loops, do a bunch of copy and pastes.

For isolating and shifting the bits, you can use divide and mod (/ and %) or masks and right shifts and the & operator like you were doing. Either way it'll be the same algorithm. But even without a loop I am only using three variables: x, digit, lastDigit.