-1

I am pretty much new to C++ and especially signed and unsigned conversions. Currently I am doing one exercise, where I need to compare a value if it is between a two values (one minimum and maximum).

For example

minimum value = -319 - it's byte array (of int8) will be {254, 193}

maximum value = 400 - it's byte array (of int8) {1, 144}

Assume the value to be compared between is -200 {255, 56}, which should be valid, but I am not able to get it correctly.

Assume the compare value, minimum and maximum value bytes is ANDed with 255 unsigned mask byte, which is then stored in int8 byte and then compared between min and max value. Due to the sign bit I am confused and not able to correctly get the comparison, but for all positive values is working fine.

The methods I tried were,

First I tried to convert int8_t to int16_t

int16_t compare = (int16_t)(inp[i] & 0xFF);

int16_t minVal = (int16_t)(minimum[i] & 0xFF);

int16_t maxVal = (int16_t)(maximum[i] & 0xFF); return (compare>=minVal&&compare<=maxVal)

Second method was I set 0 to MS bit in mask byte and then comparing each byte, since I thought comparison with that bit is not needed, but I am not sure if it is right as the data might get wrong.

Third try was to set 0 the MS Bit while comparing . I tried it and this is also a failure.

Please let me know how to compare in this scenario. Thank you

So to clarify the question I get a three 2-element byte arrays, and the function will return true, if the value byte array is inbetween min and max byte array.

So the comparison algorithm above is ran in a for loop:

bool isValueInBetween(uint8_t* min, uint8_t* max, uint8_t* val){
   bool is_match = true;
   for(int i = 0; i < 2; i++){
    int8_t compare = (inp[i] & 0xFF); //masking is used just because in the book it was mentioned, in case different mask occurs, but I think in this case it is not needed
    
    int8_t minVal = (minimum[i] & 0xFF);
    
    int8_t maxVal = (maximum[i] & 0xFF);
    if (!(compare>=minVal&&compare<=maxVal))
         is_match = false;
    }
    return is_match;
 }

The above algorithm works for positive values, but doesn't work in negative or mixed value scenarios(say min in negative and max in positive). Please let me know any good books for C++.

susil95
  • 300
  • 3
  • 5
  • 16
  • 2
    _Please let me know any good books for C++._ [The Definitive C++ Book Guide and List](https://stackoverflow.com/a/388282/7478597) – Scheff's Cat May 04 '21 at 06:05
  • 1
    Why not just convert all three values into `int16_t` then just use standard comparisons? – Alan Birtles May 04 '21 at 06:11
  • Okay, I was not sure how to do? Can I use `int16_t value; std::memcpy(&value, byte_array, sizeof(int16_t));` ? – susil95 May 04 '21 at 06:15
  • 1
    If your platform is little endian but the numbers in your byte array are stored big endian then a `memcpy()` is the wrong choice. An endian agnostic way is shown in this answer: [SO: Convert four bytes to Integer using C++](https://stackoverflow.com/a/34944089/7478597). – Scheff's Cat May 04 '21 at 07:15
  • Instead of [reposting a closed question](https://stackoverflow.com/questions/67370424/comparing-a-negative-and-positive-integers-byte-array-values-in-c), you should improve the original so it can be reopened. – Blastfurnace May 04 '21 at 12:31

1 Answers1

0

As written, the question is a little hard to understand without the exact problem statement. As of this posting, it reads as if the question was "Write a function to compare two integer values." and you took it way beyond the extreme and assumed they meant down to the binary level...

What's wrong with using the logical comparison operators directly like so:

bool isValueInBetween(int8_t val, int8_t min, int8_t max) {
    return min <= val && val <= max;
}
Casey
  • 10,297
  • 11
  • 59
  • 88
  • No, the arguments that iam getting from exercise is a three 2-byte array unsigned uint8_t arrays `uint8_t* min, uint8_t* max, uint8_t* val` – susil95 May 04 '21 at 06:16
  • 1
    @susil95 In that case, I recommend clarifying the body of your question with the exact problem statement. – Casey May 04 '21 at 06:20