Thread: Simple array and arthmetics

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    4

    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 [4] : FF 5F 09 CF

    and I have this code

    Code:
    if ((byte)(local_1[0] - 0x30U) < 10 {
       mac1 = (short) local_1[0] + -0x30;
    }
    else {
       if ((byte)(local_1[0] +0xbfU) < 6 {
       mac1 = (short) local_1[0] + -0x37;
    }
          else {
             if ((byte)(local_1[0] +0x9fU) < 6 {
             mac1 = (short) local_1[0] + -0x57;
    }
                else {
                   mac1 = 0x10
       }
     }
    }
    I would tend to say first of all that local_1[0] = 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

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    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

  3. #3
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    You can't have that code. THere are unbalanced brackets in the 'it' statements.

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

    I've made a compellable program:

    Code:
    #include <stdio.h>
    #include <stdint.h>
    typedef uint8_t byte;
    
    char local_1[4] = {0xFF, 0x5F, 0x09, 0xCF};
    
    
    int main(int argc, char *argv[]) {
       char mac1;
       if ((byte)(local_1[0] - 0x30U) < 10) {
          mac1 = (short) local_1[0] + -0x30;
       } else if ((byte)(local_1[0] +0xbfU) < 6) {
          mac1 = (short) local_1[0] + -0x37;
       } else if ((byte)(local_1[0] +0x9fU) < 6) {
          mac1 = (short) local_1[0] + -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[4] = {0xFF, 0x5F, 0x09, 0xCF};
                        ^~~~
    help.c:4:38: warning: overflow in implicit constant conversion [-Woverflow]
     char local_1[4] = {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
    Last edited by hamster_nz; 11-18-2020 at 02:35 PM.

  4. #4
    Registered User
    Join Date
    Nov 2020
    Posts
    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 : [4] = [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

  5. #5
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    (byte)(0xFF - 0x30U) = ?

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

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

  6. #6
    Registered User
    Join Date
    Nov 2020
    Posts
    4
    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[0] + 0xbfu) <6

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

    thanks

  7. #7
    Registered User
    Join Date
    Nov 2020
    Posts
    4
    Hi, any ideas about this equation ?

    I'll try out all different cases mabye . This is a tricky one

    thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple array sort.
    By milky in forum C Programming
    Replies: 5
    Last Post: 11-29-2009, 01:39 PM
  2. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 12:04 PM
  3. Simple Array or Not?
    By ajb268 in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2005, 09:51 AM
  4. Simple Encryption using an array
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 12-02-2001, 03:12 PM
  5. A simple array question
    By frenchfry164 in forum C++ Programming
    Replies: 7
    Last Post: 11-25-2001, 04:13 PM

Tags for this Thread