CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Bitwise operator

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
veerabahu



Joined: 20 Aug 2013
Posts: 16

View user's profile Send private message

Bitwise operator
PostPosted: Tue Sep 10, 2013 7:18 am     Reply with quote

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: 9245
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Sep 10, 2013 8:09 am     Reply with quote

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: 19545

View user's profile Send private message

PostPosted: Tue Sep 10, 2013 8:17 am     Reply with quote

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: 19545

View user's profile Send private message

PostPosted: Tue Sep 10, 2013 9:45 am     Reply with quote

make16, doesn't allow you to split an int32, which is the direction he wants.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group