split a long in bytes

Hello
I have a long variable with 4 bytes that i need to split to then write on a eeprom address.I need to do this convertion on a C style since it is not for using on arduino itself, it is for using on mikroC. Can someone give me a hint?
I need to write somevalues in a pic that is connect with arduino

byte1 = val & 0xff;
byte2 = (val>> 8) & 0xff;
byte3 = (val>> 16) & 0xff;

can you work out byte 4?

Grumpy_Mike:
byte1 = val & 0xff;
byte2 = (val>> 8) & 0xff;
byte3 = (val>> 16) & 0xff;
can you work out byte 4?

GM, use a code window, you know better then that. :smiley:

Can you explain me the & 0xff?

& is a bit wise AND operation, it is used to mask out or clear all the bits on the left that do not match bits set on the right.

GM, use a code window,

Well he only wanted a hint :wink:

can you work out byte 4?

byte4 = (val>> 24) & 0xff;
is this correct?

Yes! :slight_smile:

 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);

similar for 32 bit, it would be 0x0000000f, 0x000000f0, etc.
Or having bytes: 0x000000ff, 0x0000ff00, etc.

I have edited this reply. Using shift/masks can be costly in terms of code size on some controllers.
This is not code I wrote specifically for this thread, it is taken from a program where I do something similar.

& 0xff = bad, not clean

Utter tosh.
It is perfectly clear.
It is your method that is a total abomination. It is long, and totally obscure. It is clear that you have never done any real programming where you have to fit things into a fixed amount of space.
I had to manage types like you who thought they were very good coders but produced rubbish like that.

> > 4

?

Your advice is rather bazaar, I have noted in other threads you hand out bad or irrelevant advice. Remember these are mainly beginners and are easly confused.

Or for slightly better readability you could use the lowbyte macro (which replicates exactly what GM posted):

byte1= lowByte(val);
byte2= lowByte(val >> 8);

etc.

Or maybe even better readability: :wink:

byte1= lowByte(val);
byte2= highByte(val);

etc.

Please keep this one to PM if you're going to keep this up.

takao21106, I suspect English is not your first language, and judging by your earlier unorthodox and prolix post, neither is C.
Shift and mask is a perfectly acceptable way of performing what was required, and has been since before C was a twinkle in K&Rs collective eyes.

Hey everyone it's a bit fight. My money is on the old guy from Manchester. :wink:

retrolefty:
Hey everyone it's a bit fight. My money is on the old guy from Manchester. :wink:

XD Giving odds?

Somehow most of my problems are with older men in their 50s and 60s, if not all my problems altogether

I'm sorry, it's the forum's Agony Aunt's day off today - you'll have to deal with your own problems. :slight_smile:

first of all thanks for all the answers
Lets back to work :stuck_out_tongue:
I have one more question :slight_smile:
Now that I have the long splited in to bytes how can I then rearrange the long variable again?
The aim of this is:I also need to write on eeprom but after a power fail I need to get the value back from all 4 eeprom positions and recover the long variable again.

If >> took it apart, it is a good bet the opposite << will help you put it back together.
Use bitwise OR too.