470,810 Members | 1,433 Online

# Basic Bit Operation Question.

Hi every one,

I got very basic question, here i go:

Say i have 11001 11010 bits which are infact 10 bits. Now i want to
address every bit so if it is zero i would add one and if it is one
then i would be adding zero. Folks say it manchester coding.

Please note that left hand side just accept a single bit for every
operation.

how to do this , thanks.

Feb 12 '06 #1
6 2685 Jhon posted:
Hi every one,

I got very basic question, here i go:

Say i have 11001 11010 bits which are infact 10 bits. Now i want to
address every bit so if it is zero i would add one and if it is one
then i would be adding zero. Folks say it manchester coding.

Please note that left hand side just accept a single bit for every
operation.

how to do this , thanks.

I'll give a hand, rather than do the work for you.

unsigned long ManCode(unsigned long const x)
{
unsigned long result = x;

for(unsigned i = 0; i < 10; ++i)
{
if ( ! GetBit(x,i) ) ++result;
}

return result;
}

-Tomás
Feb 12 '06 #2
"Jhon" <jh**********@gmail.com> wrote in message
Hi every one,

I got very basic question, here i go:

Say i have 11001 11010 bits which are infact 10 bits. Now i want to
address every bit so if it is zero i would add one and if it is one
then i would be adding zero. Folks say it manchester coding.

Please note that left hand side just accept a single bit for every
operation.

how to do this , thanks.

I'm not quite sure what you're trying to do. Are you trying to 1's
compliment the bit stream?
11001 11010 becomes 00110 00101 ? Simply or the bits with 1.

0 | 1 = 1
1 | 1 = 0

Or are you trying to do something different?
Feb 12 '06 #3
There is an extremely sexy way to do this. Separate even from odd bits
by masking. Shift odd bits to the right by 1, so they align with the
even bits (right-most bit is 0). Now add the two.

) and shift the latter by two to the right. Add again.

11110000...) and shift by 4, add.

And so on (if you have only 10 bits, you can stop at 2x 8). Very fast,
very clever. It essentially adds individual bits in parallel. It's not
from me though;-)

Feb 12 '06 #4
Tomás wrote:
Jhon posted:
Hi every one,

I got very basic question, here i go:

Say i have 11001 11010 bits which are infact 10 bits. Now i want to
address every bit so if it is zero i would add one and if it is one
then i would be adding zero. Folks say it manchester coding.

Please note that left hand side just accept a single bit for every
operation.

how to do this , thanks.

I'll give a hand, rather than do the work for you.

unsigned long ManCode(unsigned long const x)
{
unsigned long result
for(unsigned i {
if ( ! GetBit(x,i) ) ++result;
}

return result;
}

The above routine returns the zero bit population count of its input. I
believe the original poster wants to encode a bit stream, replacing
each set bit with a set/unset bit pair and each unset bit with an
unset/set bit pair. So the bit sequence:

001100101

would be encoded as:

010110100101100110

A possible implementation (assuming stdint.h is available) would be to
encode the stream 16 bits at a time:

#include <stdint.h>

uint32_t ManCode( uint16_t x)
{
uint32_t result = 0;

for (int i = 0; i < 16; i++)
{
if (x & 0x8000)
result |= 0x02;
else
result |= 0x01;

x <<= 1;
result <<= 2;
}
return result;
}

Greg

Feb 12 '06 #5
Hi guys,
Thanks for posts, well i would just support Greg opinion. Yeah he
is quite sure how to do this and how manchester coding works.
I have solved the problem inthis way:

#define bit_get(p,m) ((p) & (m))
#define BIT(x) (0x01 << (x))

unsigned int delimiter = 0x33A // 0b11001 11010
int onebit=0;
bitcounter=9; //because it is 16 bit and i need MSB first so lets
start from bit 10;-)

while(1)
{

onebit = bit_get(delimiter, BIT((bitcounter)))

if(onebit & 0xff)
{
PORTE |= (1<<PE3);
PORTE &= ~(1<<PE3);
manchester =1;
}
else //manchester ==1
{
PORTE &= ~(1<<PE3);
PORTE |= (1<<PE3);
manchester =0;
}

onebit = 0;
bitcounter--;
if(bitcounter == 0) // just 10 times would be enough
break;
}

Feb 13 '06 #6
Jhon posted:
Hi guys,
Thanks for posts, well i would just support Greg opinion. Yeah he
is quite sure how to do this and how manchester coding works.
I have solved the problem inthis way:

#define bit_get(p,m) ((p) & (m))
#define BIT(x) (0x01 << (x))

When you use "#define" to create a function, it's called a "macro". Avoid
macros, especially when an inline function would do the trick.

template<typename T>
inline bool bit_get(T const o, unsigned const i)
{
return p & m;
}

unsigned Bit(unsigned x)
{
return 1 << x;
}

-Tomás
Feb 13 '06 #7

### This discussion thread is closed

Replies have been disabled for this discussion.