Pages: [1]   Go Down
Author Topic: unsigned Long Bug? I think so.  (Read 773 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I read in three bytes, High, Mid and low. 
Using: unsigned long tot = 0x00010000 * High + 0x0000100 * Mid + low

A B C D E F 10 11 12 13 14 15 16 17 18 19 1A
FFA FFB FFC FFD FFE FFF 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 100A
FFFA FFFB FFFC FFFD FFFE FFFF 0 1 2 3 4 5 6 7 8 9 A
1FFFA 1FFFB 1FFFC 1FFFD 1FFFE 1FFFF 20000 20001 20002 20003 20004 20005 20006 20007 20008 20009 2000A
1FFFFFA 1FFFFFB 1FFFFFC 1FFFFFD 1FFFFFE 1FFFFFF 2000000 2000001 2000002 2000003 2000004 2000005 2000006 2000007 2000008 2000009 200000A

unsigned long uLong;
void setup()
{
  Serial.begin(115200);
  Serial.println();
  for( byte i = 0; i <= 16; i++)
  {
    uLong = 0xa + i;
    Serial.print(uLong,HEX);
    Serial.print(' ');
  }
 
  Serial.println();
    for( byte i = 0; i <= 16; i++)
  {
    uLong = 0xffa + i;
    Serial.print(uLong,HEX);
    Serial.print(' ');
  }
 
    Serial.println();
    for( byte i = 0; i <= 16; i++)
  {
    uLong = 0x0fffa + i;
    Serial.print(uLong,HEX);
    Serial.print(' ');
  }
 
      Serial.println();
    for( byte i = 0; i <= 16; i++)
  {
    uLong = 0x1fffa + i;
    Serial.print(uLong,HEX);
    Serial.print(' ');
  }


  Serial.println();
    for( byte i = 0; i <= 16; i++)
  {
    uLong = 0x1fffffa + i;
    Serial.print(uLong,HEX);
    Serial.print(' ');
  }
}


void loop()
{
}

* uLongBug.ino (0.78 KB - downloaded 6 times.)
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry for my first post. Don't know why it posted, I wasn't ready.

Anyway, I wasn't getting the results I expected.  Then I discovered when the two highest bytes are zeroes (0X00001234) all math operations treat it as a 16 bit number.  If the original assigned value contains any non zero bits in the two MSBs (0x00010000) then some math operations kind of work.

My test program shows the problem in the third line below. 
I'm simply adding one and when the operation is 0x0000FFFF + 1 you get ZERO!!  I expect 0x00010000
If you add 0x0001FFFF + 1 it equals 0x00020000 as expected.

I did a search on the forum and it appears that a lot of people are having strange results with ulongs!

If I'm not thinking correctly, let me know.
TIA, john

A B C D E F 10 11 12 13 14 15 16 17 18 19 1A
FFA FFB FFC FFD FFE FFF 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 100A
FFFA FFFB FFFC FFFD FFFE FFFF 0 1 2 3 4 5 6 7 8 9 A  <<<<<<<<============ BAD DATA
1FFFA 1FFFB 1FFFC 1FFFD 1FFFE 1FFFF 20000 20001 20002 20003 20004 20005 20006 20007 20008 20009 2000A
1FFFFFA 1FFFFFB 1FFFFFC 1FFFFFD 1FFFFFE 1FFFFFF 2000000 2000001 2000002 2000003 2000004 2000005 2000006 2000007 2000008M
Logged

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 514
Posts: 26258
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

"Sorry for my first post. Don't know why it posted, I wasn't ready."
You can select Modify  & update a post if needed.

Try putting UL while setting up the variables:

uLong = 0xffaUL + i;

uLong = 0x0fffaUL + i;

uLong = 0x1fffaUL + i;

uLong = 0x1fffffaUL + i;
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Australia
Offline Offline
Jr. Member
**
Karma: 0
Posts: 99
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are using integer constants in hexadecimal form.

From "The C Programming Language" (K&R), second edition, Section A2.5.1, "Integer Constants":

... The type of an integer constant depends on its form, value and suffix...

... If it is unsuffixed octal or hexadecimal, it has the first possible of these types: int, unsigned int, long int, unsigned long int...

Note the use of "first possible", this means that without the "L" suffix to force it into a long type, it will be assigned to an int (16 bits on Arduino) if its value is within the range of ints (which it is in this case). Leading zeros are not the mechanism by which long integer constants are created, you need to explicitly suffix them.
« Last Edit: September 03, 2013, 12:05:36 am by crimony » Logged

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 514
Posts: 26258
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Dang software - performing shortcuts that aren't wanted!
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 473
Posts: 18695
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

In fact you need to be cautious about leading zeroes, what do you suppose this prints?

Code:
void setup ()
  {
  Serial.begin (115200);
  Serial.println (0001232);
  }

void loop () { }

Warning, the result may disturb some people. smiley-wink
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3941
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Nick, you are smiley-evil

smiley-wink
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

tks Brittain and others.

You fixed it.  I get caught by this about once every four yrs.  To bad I can't stick with one language. smiley-sad

Best Regards
Logged

Pages: [1]   Go Up
Jump to: