# Thread: Simple array and arthmetics

1. ## Simple array and arthmetics

Hi everyone !
I've started learning python a few months ago, but I'm stuck since a few weeks on a C script.

I've got an array :
char local_1  : FF 5F 09 CF

and I have this code

Code:
```if ((byte)(local_1 - 0x30U) < 10 {
mac1 = (short) local_1 + -0x30;
}
else {
if ((byte)(local_1 +0xbfU) < 6 {
mac1 = (short) local_1 + -0x37;
}
else {
if ((byte)(local_1 +0x9fU) < 6 {
mac1 = (short) local_1 + -0x57;
}
else {
mac1 = 0x10
}
}
}```
I would tend to say first of all that local_1 = FF

The line that i don't understand is (byte)(FF-0x30) =?

Does the result refer to the number of bits in the result of the substraction?

FF-0x30 = CF in hex = 207 in decimal = 11001111 in bin

so the answer would equal 8 ?

Thanks very much for your precious help   Reply With Quote

2. I haven't looked at your code deeply, what platform are you on?

On Intel, 'char' is a signed type, so 0xFF is -1.

On ARM 'char' is unsigned, so 0xFF is 255.

If you have all the compiler warnings turned on ("-Wall -pedantic -Wextra" options for GCC) it will point out these sorts of issues  Reply With Quote

3. You can't have that code. THere are unbalanced brackets in the 'it' statements.

And " mac1 = 0x10" is missing the semicolon.

Code:
```#include <stdio.h>
#include <stdint.h>
typedef uint8_t byte;

char local_1 = {0xFF, 0x5F, 0x09, 0xCF};

int main(int argc, char *argv[]) {
char mac1;
if ((byte)(local_1 - 0x30U) < 10) {
mac1 = (short) local_1 + -0x30;
} else if ((byte)(local_1 +0xbfU) < 6) {
mac1 = (short) local_1 + -0x37;
} else if ((byte)(local_1 +0x9fU) < 6) {
mac1 = (short) local_1 + -0x57;
} else {
mac1 = 0x10;
}
printf("mac1 is %u\n",mac1);
return 0;
}```
Here's the compilation output with warnings on:
Code:
```\$ gcc -o help help.c -Wall -pedantic
help.c:4:20: warning: overflow in implicit constant conversion [-Woverflow]
char local_1 = {0xFF, 0x5F, 0x09, 0xCF};
^~~~
help.c:4:38: warning: overflow in implicit constant conversion [-Woverflow]
char local_1 = {0xFF, 0x5F, 0x09, 0xCF};
^~~~```
As a general rule of thumb, all the cool kids working on portable system-level code are using the types in stdint.h:

uint8_t - 8-bit unsigned
uint16_t - 16-bit unsigned
uint32_t - 32-bit unsigned
uint64_t - 64-bit unsigned

int8_t - 8-bit signed
int16_t - 16-bit signed
int32_t - 32-bit signed
int64_t - 64-bit signed  Reply With Quote

4. Hi, thanks very much for your help I used ghidra to decompile an exe. this is only a small part of the code used as an example to understand.

Processor is motorola = ARM technology. So FF = 255.

I just wanted to be sure that arrays work like in python or other basic languages :  = [0,1,2,3]

and especially this simple maths result :

(byte)(0xFF - 0x30U) = ?

don't really know if i have to convert the answer etc?

thanks very much for your time   Reply With Quote

5. (byte)(0xFF - 0x30U) = ?

(byte)(0xFF - 0x30U) = 0xCF.

If that is a + or - number depends on the definition of 'byte'.  Reply With Quote

6. I guess these are all positive, but not sure.

I was also guessing that this would equal 0xCF.
But when having a look at the code, first condition is that the answer <10 . Second and third conditions are <6.
I was trying to figure out if this "10" or "6" is a decimal or decimal from signed 2nd complement answer?

So in this cas 0xCF = 207 (decimal) or -49 (decimal from signed 2nd complement)

First case (decimal answer) seems strange bcause the second "if" condition is (byte)(local_1 + 0xbfu) <6

but 0xbfu by itself in decimal is 191, so an addition with a positive number can't ever be under 6 ....

thanks   Reply With Quote

I'll try out all different cases mabye . This is a tricky one   Reply With Quote
Popular pages Recent additions bytelocal_1, local_1, mac1, result, short 