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:

#include <iostream>
using namespace std;

int main()
{
    unsigned long int n;
    cin >> n;
    int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0, k = 0;
    int mask = 0xF;
    a = n & mask;
    n = n >> 4;
    b = n & mask;
    n = n >> 4;
    c = n & mask;
    n = n >> 4;
    d = n & mask;
    n = n >> 4;
    e = n & mask;
    n = n >> 4;
    f = n & mask;
    n = n >> 4;
    g = n & mask;
    n = n >> 4;
    h = n & mask;

    return 0;
}

if anyone can tell me how to continue... I'll be grateful!

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...

  1. Start with number x with some value: AAAA
  2. Split x into two separate parts: the last hex digit and everything but that: AAA|A
  3. Stick the AAA into the variable x and have another variable called digit, so x = AAA, digit = A.
  4. If x is 0, we're done. All the hex digits match. In this case, x is AAA, not 0, so we continue.
  5. 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.
  6. Compare digit and lastDigit. If they are different, then you're done: at least one hex digit does not match.
  7. 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.

Edited 2 Weeks Ago by AssertNull