View previous topic :: View next topic |
Author |
Message |
karthickiw
Joined: 09 Aug 2007 Posts: 82 Location: TN, India
|
how to convert 8 bit to one byte |
Posted: Tue Jun 16, 2009 11:57 am |
|
|
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
|
|
|
John P
Joined: 17 Sep 2003 Posts: 331
|
|
Posted: Tue Jun 16, 2009 2:02 pm |
|
|
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
|
OK - heap some abuse on me |
Posted: Tue Jun 16, 2009 4:42 pm |
|
|
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
|
Re: how to convert 8 bit to one byte |
Posted: Wed Jun 17, 2009 2:24 am |
|
|
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
|
|
Posted: Wed Jun 17, 2009 7:44 am |
|
|
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
|
|
Posted: Wed Jun 17, 2009 8:22 am |
|
|
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
|
|
Posted: Wed Jun 17, 2009 11:52 am |
|
|
What I said also, but perhaps I was being a little obscure in the explanation. |
|
|
|