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

how to convert 8 bit to one byte

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



Joined: 09 Aug 2007
Posts: 82
Location: TN, India

View user's profile Send private message Send e-mail

how to convert 8 bit to one byte
PostPosted: Tue Jun 16, 2009 11:57 am     Reply with quote

hi friends,
How to convert 8 bit of data to one byte of data?
For example,

input:
Code:

bit b[0]=1;
bit b[1]=1;
bit b[2]=1;
bit b[3]=1;
bit b[4]=1;
bit b[5]=1;
bit b[6]=1;
bit b[7]=1;

After converting the 8 bits to byte, I need 0XFF answer.
Please give some idea or example coding...
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jun 16, 2009 12:33 pm     Reply with quote

Use a Union of bits and a byte:
http://www.ccsinfo.com/forum/viewtopic.php?t=28773

Use Treitmey's code, except don't swap the order of the bits:
http://www.ccsinfo.com/forum/viewtopic.php?t=23364
John P



Joined: 17 Sep 2003
Posts: 331

View user's profile Send private message

PostPosted: Tue Jun 16, 2009 2:02 pm     Reply with quote

This probably isn't the right solution for every situation, but could you deliberately use the individual bits in a byte all along, rather than having the compiler assign them in an unknown location, and then having to create the byte explicitly?
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

OK - heap some abuse on me
PostPosted: Tue Jun 16, 2009 4:42 pm     Reply with quote

ok - abuse me - I have never yet bothered with any fancy structures for what I believe is the special , trivial case of mapping a bit to a byte -
MINUS all the extra syntactical wrapping of the way it has been described in other postings.

( SURE - for more complicated structures - I get it-
but BITS in a BYTE ?? come on now)

I have several projects that use external latches - and I map various functions to the bits. This works equally well with a hardware port too.
the code is :
Code:

unsigned int8 feature=0b11001010;
#bit lovolt = feature.0
#bit hivolt = feature.1
#bit oscoff = feature.2
#bit detmute= feature.3
#bit tpo_warn=feature.4
#bit cabalarm=feature.5
#bit pll_flag=feature.6
#bit sparea  =feature.7

Then you just refer to the bits by name
as in lovolt=1
oscoff=0;
if (cabalarm) {}

and so on - etc

The resultant code for flipping the bits is a one cycle , single instruction too - and I find it MUCH simpler to handle.

OK pile on - why is this approach a bad idea?
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

Re: how to convert 8 bit to one byte
PostPosted: Wed Jun 17, 2009 2:24 am     Reply with quote

karthickiw wrote:
hi friends,
How to convert 8 bit of data to one byte of data?
For example,

input:
Code:

bit b[0]=1;
bit b[1]=1;
bit b[2]=1;
bit b[3]=1;
bit b[4]=1;
bit b[5]=1;
bit b[6]=1;
bit b[7]=1;

After converting the 8 bits to byte, I need 0XFF answer.
Please give some idea or example coding...


It may be trivial to map bits to a byte but you are infact converting an array of values to a byte. This is somewhat different to bit mapping!

To convert this to a byte you could use

Code:

int i;
int val;
for (i = 0; i < 8; i++)
{
  val =(val << 1) + b[7 - i];
}


But as stated there may be better ways to do this and just because you have not used structs does not mean they are not a better option.

Your second example actually shows you using a bitmap (feature), I am not sure why you think these 2 examples are the same.

Bit mapping refers to having a variable of size greater than 1 bit and mapping values to one or more bits of the variable. You can then reference the bit/s by using masks and shifting left or right to modify the bitmap and retrieve the relevent values.
Guest








PostPosted: Wed Jun 17, 2009 7:44 am     Reply with quote

asmboy - I'm with you - that is exactly how I code too. I assign bits to a name then just assign the name to a 0 or 1 in my code as required - I even write my own SPI using this method (Bit-Bang outputs Gasp!).

The variable is just the port pin.

My 2 cents worth - Steve H.
Ttelmah
Guest







PostPosted: Wed Jun 17, 2009 8:22 am     Reply with quote

Only reason it is a 'bad idea', is that it is CCS specific....

The alternative is a union/structure. You just create eight bitfields, and then use a union to join this to the byte. Except for having to handle the possibilities of bits being assigned in different orders on different chips, this works with any C. Both generate single cycle operations,if the compiler is smart.

I must admit, for 'hardware specific' operations like the one described by asmboy, I'd probably go with the bit declaration. Because it is hardware specific, it is going to need 're-writing' to go onto any other chip, and it is simple to code, and use. Hence KISS.

Best Wishes
John P



Joined: 17 Sep 2003
Posts: 331

View user's profile Send private message

PostPosted: Wed Jun 17, 2009 11:52 am     Reply with quote

What I said also, but perhaps I was being a little obscure in the explanation.
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