View previous topic :: View next topic |
Author |
Message |
veerabahu
Joined: 20 Aug 2013 Posts: 16
|
Bitwise operator |
Posted: Tue Sep 10, 2013 7:18 am |
|
|
How do i Split 32bit value into two 16bit value..i use the format for bit wise operator (Value & 0x0000FFFF) actually i use calculator for this i got correct value but when i use this format in coding i got wrong answer |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9241 Location: Greensville,Ontario
|
|
Posted: Tue Sep 10, 2013 8:09 am |
|
|
easy !
Look in the compiler help files( press F11 when project is open), and click on 'make32()' function, located in builtin function, all builtin functions.
There's also make8() as well as make16() functions.
Great thing is CCS SHOW how to use them !
hth
jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Tue Sep 10, 2013 8:17 am |
|
|
Save a lot of work. Just use a union....
Code: |
struct words {
int16 low;
int16 high;
};
union {
int32 longword;
struct words bits16;
} value;
//Then
value.longword //is the 32bit value
value.bits16.low //is the low 16bits
value.bits16.high //is the high 16 bits
//You can read and write to the whole thing, or the parts at will.
|
Value & 0x0FFFF should happily return the low 16 bits, provided 'value' is an int32, but involves a lot more work in code terms than using the union, while the high 16bits then involves rotating the data as well....
There are several other ways of doing this (pointers, using #word to locate a variable for each half, etc.), but the union is one of the most reliable/easiest.
Best Wishes |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Tue Sep 10, 2013 9:45 am |
|
|
make16, doesn't allow you to split an int32, which is the direction he wants.
Best Wishes |
|
|
|