Thread: 2's compliment sign bit extension

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    10

    2's compliment sign bit extension

    Hello.
    I am trying to read data from a 24 bit ADC which gives out data in 2's compliment 24 bits but I would like to represent this in 32 bit, with the sign bit extended. I am having some trouble writing some code to extend this sign bit, is the following okay? what could I do or change, or if I can use other things like for loops, my coding skills are super basic so please be patient with me!

    Code:
    long adcvalue;
    uint32_t adcvalue_32bit;
    unsigned long bit24;
    long X;
    
    
    void readADS1231(void) 
      {
        adcvalue = 0;  
        X = 0; 
        bit24 = 0x800000; // sign bit of the 24 bit adcvalue - bit 24 ( or 23rd bit) 
        adcvalue_32bit = 0;
           
        while(digitalRead(D_OUT));//WAIT UNTIL DATA IS READY
        //24-->1
        clock ();
        if(digitalRead(D_OUT)){adcvalue=adcvalue+0x800000;} //24
        clock ();
        if(digitalRead(D_OUT)){adcvalue=adcvalue+0x400000;}//23
        // Bit 24 all the way down to bit 1:
        //..............................................................................
        //..............................................................................
        clock ();
        if(digitalRead(D_OUT)){adcvalue=adcvalue+0x1;}//1
        clock ();
       
        // Sign Extending adcvalue (24 bit) to 32 bit
    
    
        X = bit24 & adcvalue; // zero-ing the rest of the adcvalue bits except the sign bit.
        if(X==bit24) // if sign bit is = 1
        {
          adcvalue_32bit = adcvalue+0xFF000000; // sign extend adc value to 32 bit
        }
        else        // sign bit is = 0
        {
          adcvalue_32bit = adcvalue+0x00000000; // sign extend adc value to 32 bit
        } 
        //  
        digitalWrite(CLOCK, LOW);   
       }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    You could turn all that repeating code into a for loop, like this:
    Code:
    for (i = 0x800000; i != 0; i >>= 1) {
        clock();
        if (digitalRead(D_OUT)) {
            adcvalue += i;
        }
    }
    Also, why so many global variables? I don't know much about arduino programming, doesn't it have a stack to use local variables on?
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Depending on the compiler and signed shifting, this might work:

    Code:
    /* ... */
        adcvalue = ((long) (((unsigned long)adcvalue)<<7))>>7;

  4. #4
    Registered User
    Join Date
    Sep 2017
    Posts
    10
    Thank you both for your replies.

    Basically this is what I have in 24 bit:
    // two's comp min = 0x800000 = -8388608; What I want it to be after it is converted = 0
    // two's comp zero = 0x000000 = 0; What I want it to be after it is converted = 2^23 = 8,388,608
    // two's comp max = 0x7FFFFF = +8388607; What I want it to be after it is converted = (2^24)-1 = 16,777,215
    The problem comes when I extend the sign bit and turn it into a 32 bit, things just get a bit confusing for me
    When I extended the sign bit this is what I think it will turn into?:
    // two's comp min = 0xFF800000 = -4286578688; What I want it to be after it is converted = 0
    // two's comp zero = 0x00000000 = 0; converted = 2^31 = 2,147,483,648 (Max/2)
    // two's comp max = 0x007FFFFF = +8388607; converted = (2^32)-1 = 4294967295 ??

    If all this is okay, to impliment this do I just add 4286578688 to my 32bit adc values?
    e.g uint32_t scaledval = adcvalue_32bit + 0xFF800000;

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    According to this webpage, the arduino language does sign extension for signed integers, which means the solution @rcgldr proposed should work.
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Sep 2017
    Posts
    10
    Quote Originally Posted by GReaper View Post
    According to this webpage, the arduino language does sign extension for signed integers, which means the solution @rcgldr proposed should work.
    Sorry I do not understand that solution. What exactly is it doing? is that supposed to replace my code:
    Code:
     X = bit24 & adcvalue; // zero-ing the rest of the adcvalue bits except the sign bit.
        if(X==bit24) // if sign bit is = 1
        {
          adcvalue_32bit = adcvalue+0xFF000000; // sign extend adc value to 32 bit
        }
        else        // sign bit is = 0
        {
          adcvalue_32bit = adcvalue+0x00000000; // sign extend adc value to 32 bit
        } 

    or what exactly? and if it is t replace the code above, how do I then do the conversion to the correct scale? is that just going to be a matter of uint32_t scaledval = adcvalue_32bit + 0xFF800000 or something else?

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    I don't know what you mean by "conversion to the correct scale", but yes, @rcgldr's code converts a 24-bit signed integer into a 32-bit signed integer by using sign extension. Your code works fine too, don't get me wrong.

    EDIT: Never mind, now I understand. What you need to do is to simply add the minimum value. Say, for example:
    Code:
    // From [-8388608, 8388607] to [0, 16777215]
    uint32_t scaledval = adcvalue_32bit + 0x800000;
    Last edited by GReaper; 09-28-2017 at 05:39 AM.
    Devoted my life to programming...

  8. #8
    Registered User
    Join Date
    Sep 2017
    Posts
    10
    Thank you all, It works fine now! I guess I just have to pay attention to data types in the future!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. regarding 1's compliment
    By basva in forum C Programming
    Replies: 1
    Last Post: 11-04-2011, 01:00 AM
  2. unwanted sign extension
    By qqqqxxxx in forum C Programming
    Replies: 5
    Last Post: 02-14-2006, 05:18 AM
  3. hex 16's compliment
    By xddxogm3 in forum Tech Board
    Replies: 9
    Last Post: 09-13-2005, 11:29 PM
  4. sign extension char
    By Laserve in forum C Programming
    Replies: 3
    Last Post: 04-28-2005, 08:28 AM
  5. Would you use an IRC server to compliment these forums?
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 03-15-2002, 04:00 AM

Tags for this Thread