Go Down

Topic: split a long in bytes (Read 3031 times) previous topic - next topic

lloyddean

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

Should

Nick Gammon


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


Yes but Mike, maybe the people on the PIC forum are all young. :)
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Grumpy_Mike

:)
yes probbly just like you know who!


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.
I also have PIC related web domain.

Professor Chaos

#34
Mar 13, 2012, 03:27 am Last Edit: Mar 13, 2012, 03:33 am by Professor Chaos Reason: 1



Code: [Select]

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: [Select]
  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.


Groove

Can anyone explain the code in reply #8, please?
Per Arduino ad Astra

Nick Gammon

No, because it is Complete Nonsense.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon

Code: [Select]
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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

dxw00d

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


It's not just me then. Good.

Grumpy_Mike

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?

Nick Gammon


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


Code: [Select]
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: [Select]
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?
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

HugoPT

Code: [Select]

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
Debian,Mint,Ubuntu
Arduino Mega 2560
Arduino Nano
Arduino Duemilanove
MAC OS Montain Lion
Raspberry PI Model B

Groove

Quote
Well it leads to this:

Well, yes, obviously, but even if it had been ">>" and not "> >", what is it supposed to do?
Per Arduino ad Astra

Grumpy_Mike

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: [Select]

   // 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.

Go Up