Thread: Signed and Unsigned Variables

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    35

    Signed and Unsigned Variables

    I am trying to calculate the RMS value, but i need to offset by 512,
    I get the raw value from 0 to 1024, i need subtract 512 from this number and then square the number, but i get warnings

    Code:
    void Rms(uint16 rawcount)
    {
    uint16_t l_temp_rawcount_u16 = 0;
    uint32_t l_temp_square_u32=0;
    int16_t l_temp_offset_s16 =0;
    l_temp_rawcount_u16  = rawcount;
    
    l_temp_offset_s16  = l_temp_rawcount_u16   - 512u;
    l_temp_square_u32 = l_temp_offset_s16   * l_temp_offset_s16  ;
    }
    I get warning on the last two lines,
    warning: implicit conversion changes signedness: 'unsigned int' to 'int16_t' (aka 'short') [-Wsign-conversion]

    warning: implicit conversion changes signedness: 'int' to 'uint32_t' (aka 'unsigned long') [-Wsign-conversion]

  2. #2
    Registered User
    Join Date
    Apr 2019
    Posts
    35
    I tried with the following code
    Code:
    void Rms(uint16 rawcount)
    {
      uint16_t l_temp_rawcount_u16 = 0;
      uint32_t l_temp_square_u32 = 0;
      int16_t l_temp_offset_s16 = 0;
      l_temp_rawcount_u16 = rawcount;
    
      l_temp_offset_s16 = (int16_t) (l_temp_rawcount_u16 - 512u);
      l_temp_square_u32 = (uint32_t) (l_temp_offset_s16 * l_temp_offset_s16);
    }
    The warning is not coming. Is it correct?
    Last edited by Salem; 04-30-2022 at 01:21 AM. Reason: Removed crayola

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Start with
    int32_t l_temp_rawcount = (int32_t)rawcount;
    int32_t l_temp_offset = l_temp_rawcount - 512;


    > l_temp_square_u32 = (uint32_t) (l_temp_offset_s16 * l_temp_offset_s16);
    Here, the parenthesised expression is evaluated in 16 bits, risking overflow, and it's only the final result that is promoted.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    35
    Ok i understand now it shall be like this
    Code:
    void Rms(uint16 rawcount_u16)
    {
    uint16_t l_temp_rawcount_u16 = 0;
    uint32_t l_temp_square_u32=0;
    int32_t l_temp_offset_s32 =0;
    l_temp_rawcount_u16  = rawcount_u16;
    
    l_temp_offset_s32 = (int32_t)(l_temp_rawcount_u16   - 512u);
    l_temp_square_u32 = uint32_t(l_temp_offset_s32 * l_temp_offset_s32);
    }
    I hope this is correct?

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    35
    Ok one point i forgot to mention is that rawcount can be maximum is 1024.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You may as well promote the input directly to int32_t
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Apr 2019
    Posts
    35
    I hope, this is what you mean?
    Code:
    void Rms(uint16 rawcount_16)
    {
    uin32_t l_temp_square_u32=0;
    int32_t l_temp_offset_s32=(int32_t)rawcount_16;
    l_temp_offset_s32 -= 512;
    l_temp_square_u32 = (uin32_t)(l_temp_offset_s32 * l_temp_offset_s32 );
    }

    Other doubt is this is related to micro controller based programming, defining a variable if higher bit size than required, will save some execution cycles? since i write this in interrupt? i feel that Signed32 seem to be larger, it can maximum go to 1024 signed value l_temp_offset_s32 .

  8. #8
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    Your function as written does nothing since all it does is set a local variable and doesn't return anything. Also, get rid of the stupid u32/s32/etc suffixes. How about:
    Code:
    uint32_t Rms(uint16_t rawcount)
    {
        int32_t count = (int32_t)rawcount - 512;
        return (uint32_t)(count * count);
    }
    Last edited by john.c; 04-30-2022 at 01:02 PM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a question about signed and unsigned variables
    By Mohsen Abasi in forum C Programming
    Replies: 2
    Last Post: 05-29-2017, 08:02 AM
  2. Replies: 3
    Last Post: 05-24-2015, 12:24 PM
  3. Why can't 'float' variables be assigned to signed or unsigned?
    By cplusplusnoob in forum C++ Programming
    Replies: 2
    Last Post: 03-26-2012, 12:02 PM
  4. Signed and unsigned
    By ncode in forum C Programming
    Replies: 3
    Last Post: 09-02-2011, 08:59 PM
  5. signed/unsigned int
    By X PaYnE X in forum C Programming
    Replies: 3
    Last Post: 06-10-2004, 10:58 AM

Tags for this Thread