Pages: 1 2 [3]   Go Down
Author Topic: split a long in bytes  (Read 1951 times)
0 Members and 1 Guest are viewing this topic.
Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

"mikroC PRO for PIC is a full-featured ANSI C compiler for PIC devices from Microchip®."

Should
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well bit of a nerve asking about a PIC on an arduino forum, but never mind.  smiley

Yes but Mike, maybe the people on the PIC forum are all young. smiley
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 514
Posts: 31567
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

 smiley
yes probbly just like you know who!
Logged

IRL
Offline Offline
Full Member
***
Karma: 0
Posts: 146
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Your advice is rather bazaar, I have noted in other threads you hand out bad or irrelevant advice.

I also sometimes contributed to a solution, here, and on other forums.
I agree my original reply was a quick judgement and I removed sentences/replies
that are not contributing to a solution and/or irrelevant to the thread topic.

Unions and typecasts (see my reply) are archieving a similar thing, in the end.
I used assembler for some years not only for MCUs, and currently I move away from it,
because the poor maintainability. I am here on the forum basically also to learn from others.

There is one assembler source about 1 megabyte which I browsed some months ago the largest I wrote is only 300k unfortunately it is lost. Isn't one great feature of C the portability, you can understand Atmel sources, or PIC sources, even if you don't use it currently?

Quote
Remember these are mainly beginners and are easly confused.

I'd second that, people out there are surprisingly intelligent at times, while over time, not all of them will become programmers or even professional users.

They'd use the advice that is most appreciate to them, and more or less ignore the others.
Logged

I also have PIC related web domain.

0
Offline Offline
Full Member
***
Karma: 1
Posts: 225
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset



Code:
piece =( eeprom_byte4 << 24) | (eeprom_byte3 << 16) | (eeprom_byte2 << 8) | eeprom_byte1;


Mike, isn't there an issue with that code?  Namely, that the two highest bytes will be lost because the compiler will treat eeprom_byte4 << 24 and eeprom_byte3 << 16 as int expressions without an explicit cast?

E.g., the following code:
Code:
  unsigned long foo;
  byte bar = 255;

  foo = ( bar << 24) | (bar << 16);
  Serial.println(foo,DEC);

  foo = ((unsigned long)bar << 24) | ((unsigned long)bar << 16);
  Serial.println(foo,DEC);

Produces output of:

0
4294901760


.... showing that the bits are lost without the cast.

« Last Edit: March 12, 2012, 09:33:48 pm by Professor Chaos » Logged

UK
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2883
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can anyone explain the code in reply #8, please?
Logged

Per Arduino ad Astra

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No, because it is Complete Nonsense.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if((phase>>1)==0)led_data=led_numbers&0x000f;

What is phase? Why is it shifted right 1 bit? I guess we will never know.
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Can anyone explain the code in reply #8, please?

It's not just me then. Good.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 514
Posts: 31567
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Mike, isn't there an issue with that code?  Namely, that the two highest bytes will be lost because the compiler will treat eeprom_byte4 << 24 and eeprom_byte3 << 16 as int expressions without an explicit cast?
It depends on how these variables were defined. If they were defined as longs then there will be no problem.
If they were defined as bytes or ints I would expect the compiler to automatically promote them to longs because the left hand side is a long. However I know that some compilers do this while others do not. Good point have you tried it?
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can anyone explain the code in reply #8, please?

Code:
if((phase>>1)==0)led_data=led_numbers&0x000f;
   if((phase>>1)==1)led_data=((led_numbers&0x00f0) > > 4);
   if((phase>>1)==2)led_data=((led_numbers&0x0f00) > > 8);
   if((phase>>1)==3)led_data=((led_numbers&0xf000) > > 12);

Well it leads to this:

Code:
sketch_mar13a:8: error: expected primary-expression before '>' token
sketch_mar13a:9: error: expected primary-expression before '>' token
sketch_mar13a:10: error: expected primary-expression before '>' token

Now do you understand?
Logged

Portugal
Offline Offline
Edison Member
*
Karma: 28
Posts: 1436
Tomorrow I will know a BIT more than yesterday
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
unsigned long foo;
  byte bar = 255;

  foo = ( bar << 24) | (bar << 16);
  Serial.println(foo,DEC);

  foo = ((unsigned long)bar << 24) | ((unsigned long)bar << 16);
  Serial.println(foo,DEC);
I confirm it realy need the cast
Thanks Professor Chaos for the tip
Logged

Debian,Mint,Ubuntu
Arduino Mega 2560
Arduino Nano
Arduino Duemilanove
MAC OS Montain Lion
Raspberry PI Model B


UK
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2883
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Well it leads to this:
Well, yes, obviously, but even if it had been ">>" and not "> >", what is it supposed to do?
Logged

Per Arduino ad Astra

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 514
Posts: 31567
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
what is it supposed to do?
Well it was in response to the question of splitting a long into bytes,
What it is supposed to do and what it actually does are two different matters
correcting the shift what it does do is:-

Code:
   // the variable phase will decide which nibble (not byte) is extracted from the long variable led_numbers
  // so if phase is equal to 0 or 1 it will do this first line
   if((phase>>1)==0)led_data=led_numbers&0x000f; // put the least significant nibble into led_data

  // if phase is equal to 2 or 3 it will do this next line
   if((phase>>1)==1)led_data=((led_numbers&0x00f0) >> 4); // put the next most significant nibble into led_data

   // if phase is equal to 4 or 5 it will do this next line
   if((phase>>1)==2)led_data=((led_numbers&0x0f00) >> 8); // put the next most significant nibble into led_data

  // if phase is equal to 6 or 7 it will do this next line
   if((phase>>1)==3)led_data=((led_numbers&0xf000) >> 12); // put the next most significant nibble into led_data

So depending on the value of phase it will extract a nibble from variable. Not quite what was asked for.
Logged

Pages: 1 2 [3]   Go Up
Jump to: