user15072924
user15072924

Reputation: 11

how to define uint variable as -1 in c#

I have the following function written in c and working properly; but I need to convert it to c#. There is variable called unsigned int found_at = -1 ; the equivalent in c# should be uint found_at = -1; However I receive compiler error. Could you please guide me to solve this problem

unsigned int find_adjacent_values(double val, unsigned int start_here, unsigned int end_here, double *vector) {
unsigned int i = start_here, found = 0, found_at = -1;
while (i < end_here-1 && found == 0) {
    if (val >= vector[i] && val < vector[i+1]) { // check whether we found adjacent values
        found = 1;
        found_at = i;
    } else {
        i += 1;
    }
}
return found_at;

}

Upvotes: 0

Views: 945

Answers (1)

xanatos
xanatos

Reputation: 111930

Try:

uint found_at = unchecked((uint)-1);

Note that thanks to how two's complement numbers work, that line is equivalent to writing:

uint found_at = uint.MaxValue;

The explanation about why you need the unchecked(...) is here.

Upvotes: 1

Related Questions