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 support@ccsinfo.com

splitting a binary string

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







splitting a binary string
PostPosted: Thu Apr 03, 2003 10:03 am     Reply with quote

Hi Folks,
have a fairly basic knowledge of C. I need to take a binary string:
0001000110
cut off the last 4 bits i.e. 0110
store it as X1 and compare it to a previously stored value of X2, if X1 is greater than X2 then store X1 into X2.
any ideas,
Thanks kieran.
___________________________
This message was ported from CCS's old forum
Original Post ID: 13361
neil



Joined: 08 Sep 2003
Posts: 128

View user's profile Send private message

Re: splitting a binary string
PostPosted: Thu Apr 03, 2003 10:25 am     Reply with quote

Hi Kieran, I think the easiest way would be to do this:
(assuming 'X1' is the four LSBs)

o Take the string 00 0100 0110
o Apply an 'AND' mask to it...
0000 0100 0110
AND 0000 0000 1111
= 0000 0000 0110
o Code: X1 = string & 0x006;

OR... Assuming you want to *ignore* the four LSBs...

o Take the string and shift it four places to the RIGHT
eg. X1 = string >>4;

This will push the four LSBs off the end, moving the data you want to the LSB position. The MSB end of the word will have '0' shifted in, so the word will become '0000 0000 0100'

I hope this explains what you were asking!
Neil.


:=Hi Folks,
:=have a fairly basic knowledge of C. I need to take a binary string:
:=0001000110
:=cut off the last 4 bits i.e. 0110
:=store it as X1 and compare it to a previously stored value of X2, if X1 is greater than X2 then store X1 into X2.
:=any ideas,
:=Thanks kieran.
___________________________
This message was ported from CCS's old forum
Original Post ID: 13362
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: splitting a binary string
PostPosted: Thu Apr 03, 2003 3:19 pm     Reply with quote

:=Hi Folks,
:=have a fairly basic knowledge of C. I need to take a binary string:
:=0001000110
:=cut off the last 4 bits i.e. 0110
:=store it as X1 and compare it to a previously stored value of X2, if X1 is greater than X2 then store X1 into X2.
----------------------------------------------------

You've stated the problem very well. That's 90\% of the
solution, right there. I think the knowledge you're missing
is how to mask off some bits. But that's easy. Use the
bitwise AND operator. Then just take your words, and translate
them into code.

<PRE>
main()
{
char c;
char x1;
char x2;
<BR>
// Get a value into c, somehow.
<BR>
<BR>
// Save lower 4 bits of c into x1. (Clear the upper 4 bits).
x1 = c & 0x0f;
<BR>
// Compare x1 to x2. If x1 is > x2, then store x1 into x2.
if(x1 > x2)
x2 = x1;
<BR>
while(1);
}
</PRE>
___________________________
This message was ported from CCS's old forum
Original Post ID: 13380
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