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

Simple (for you) binary conversion

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



Joined: 04 Jun 2009
Posts: 107

View user's profile Send private message

Simple (for you) binary conversion
PostPosted: Thu Jun 04, 2009 11:33 am     Reply with quote

Hi. First... I'm not a programmer. I've managed my way through perl and php to develop some websites, and I've done some basic modification of code in C++ but that's the extent of it. So I'm pseudo-comfortable in the code world, but I completely lack the foundation.

I have some source code and its buggy. I need to make a few small modifications and I hope you can help.

I have a variable with some hex value. Let's say its 0x24 at the moment. In binary that's:

0x24 = 0010 0100

I need to "activate the 5th and 6th bit" of that variable. The code right now adds +40 to that variable, which in the case of 24 is correct:

0x64 = 0110 0100

But it fails when the variable is something like 0x0F

0x0F = 0000 1111
0x4F = 0100 1111 <-- + 40
0x6F = 0110 1111 <-- 5th and 6th bit activated

I imagine there's got to be some slick operator that I can directly manipulate the bits with, right?

Thanks!

Steve
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jun 04, 2009 11:43 am     Reply with quote

Are you using the CCS C compiler for PIC microcontrollers ?

If so, post a short test program that shows your code.
John P



Joined: 17 Sep 2003
Posts: 331

View user's profile Send private message

PostPosted: Thu Jun 04, 2009 11:48 am     Reply with quote

This is a trick question, right?

"Activate" could mean set high or set low. It has no defined meaning in programming. People might casually use it in the sense that some external device requires, and that could be either state.

"5th and 6th bits" could mean bits 5 and 6, or bits 4 and 5. I'm not being totally pedantic here--which is the "first bit" of a byte? So which is the "5th bit"? Much better to call them "bit 0" up to "bit 7".

Anyway, you could write it as

s_macks_variable |= 0x60;
// Logical OR desired data into byte

or

bit_set(s_macks_variable, 5);
bit_set(s_macks_variable, 6);
// Set the two bits as required
s_mack



Joined: 04 Jun 2009
Posts: 107

View user's profile Send private message

PostPosted: Thu Jun 04, 2009 11:54 am     Reply with quote

Yes, that's right. Its a PIC18F4480 microcontroller. Using MPLAB IDE (8.02 I think) with CCS plug-in.

Here's the snippet in question:
Code:
void ReportST(int8 mode, int8 id, int8 value)
{
      st_msg.canid = 0x7E8;
      st_msg.data[0] = 0x04;  //a requirement of the device. It wants to know how much data (after this) is coming
      st_msg.data[1] = mode + 0x40; //a requirement of the device. It wants the mode returned + 40 [actually 5th and 6th bit set]
      st_msg.data[2] = id;
      st_msg.data[3] = value;
      st_msg.data[4] = 0x01;   //just a little "success" message that the device utilizes
      st_msg.len = 5;
      SendStatusMessage(st_msg);
}


Thanks
s_mack



Joined: 04 Jun 2009
Posts: 107

View user's profile Send private message

PostPosted: Thu Jun 04, 2009 11:57 am     Reply with quote

John P wrote:
This is a trick question, right?

"Activate" could mean set high or set low. It has no defined meaning in programming. People might casually use it in the sense that some external device requires, and that could be either state.

"5th and 6th bits" could mean bits 5 and 6, or bits 4 and 5. I'm not being totally pedantic here--which is the "first bit" of a byte? So which is the "5th bit"? Much better to call them "bit 0" up to "bit 7".

Anyway, you could write it as

s_macks_variable |= 0x60;
// Logical OR desired data into byte

or

bit_set(s_macks_variable, 5);
bit_set(s_macks_variable, 6);
// Set the two bits as required


Fair enough. I said I'm not a programmer so I'm probably not using perfect terminology. Some of that I'm using from specifications.

From examples, I can say that "activate" means make it a 1, not a zero. And "5th and 6th" (their words, not mine) are zero-based and refer to bit 5 and 6 as my examples showed.

So does bit_set make it a 1? Or does it change it from whatever it is?
bungee-



Joined: 27 Jun 2007
Posts: 206

View user's profile Send private message

PostPosted: Thu Jun 04, 2009 12:06 pm     Reply with quote

s_mack wrote:


Fair enough. I said I'm not a programmer so I'm probably not using perfect terminology. Some of that I'm using from specifications.

From examples, I can say that "activate" means make it a 1, not a zero. And "5th and 6th" (their words, not mine) are zero-based and refer to bit 5 and 6 as my examples showed.

So does bit_set make it a 1? Or does it change it from whatever it is?

bit_set sets bit to 1. If it is allready set there is no change. Realy simple bit modification when you need to set specific bits to 1 is OR function as it has been writen two posts up. (Example: variable |= 0b01100000; ) when you need to set zeros in some place you use the AND function (Example: variable &= 0b10011111; )
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jun 04, 2009 12:07 pm     Reply with quote

Code:
st_msg.data[1] = mode + 0x40;

But it fails when the variable is something like 0x0F

0x0F = 0000 1111
0x4F = 0100 1111 <-- + 40
0x6F = 0110 1111 <-- 5th and 6th bit activated

It should not be doing this.

If it is doing this, you need to make a test program to study the issue
and find out why. The test program will perform the operation in
question, and display the output.

The code you posted was a code fragment, not a test program.

This thread has an example of a test program. His initial program is
much too large to test the problem. I created a small program that
tests if a 2-dimensional array can be passed to a function, and displayed
properly.
http://www.ccsinfo.com/forum/viewtopic.php?t=34909
s_mack



Joined: 04 Jun 2009
Posts: 107

View user's profile Send private message

PostPosted: Thu Jun 04, 2009 12:29 pm     Reply with quote

Thank you very much bungee and John. That was the ticket.

PCM Programmer - I'm not sure you understood what I was asking. Sorry for the confusion.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jun 04, 2009 12:37 pm     Reply with quote

OK, now I get it. When you said it "fails", you didn't mean a compiler
bug. You just wanted to know how to set a bit.
s_mack



Joined: 04 Jun 2009
Posts: 107

View user's profile Send private message

PostPosted: Thu Jun 04, 2009 12:44 pm     Reply with quote

Yeah, sorry Smile Careless use of words. I meant that the code as written "failed" my criteria of how I wanted the variable manipulated. Computationally, of course, the code did exactly what it was told to do. I simply wanted to tell it to do something different to achieve a desired outcome.

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