Basic Bit Operation Question.

J

Jhon

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

Rod Pemberton

Jhon said:
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[sic,insert] one and if it is one
then i would be adding[sic,inserting] zero. Folks say it manchester coding.

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

If you have binary 11001 11010, you want it converted to binary 1010010110
1010011001. It that correct?

Rod Pemberton
 
R

Rod Pemberton

Rod Pemberton said:
Jhon said:
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[sic,insert] one and if it is one
then i would be adding[sic,inserting] zero. Folks say it manchester coding.

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

If you have binary 11001 11010, you want it converted to binary 1010010110
1010011001. It that correct?

If you can separate your data into nybbles (4-bits), the routine below
converts to Manchester. It's not fast or efficient or easy to use with
5-bits or 10-bits. If your wanting to use Manchester for something serious,
you'll need a better algorithm or a lookup table. Basically, Manchester
data is an interleave of the bits of a value with the inverted bits of that
value:

data 11001 11010
inverted 00110 00101
interleaved 1010010110 1010011001

#include <stdio.h>
#include <stdlib.h>

unsigned short man_nyb(unsigned short value)
{
unsigned short v1,v2;
char i;

v1=value&0x0F;
v2=v1^0x0F;
value=0;
for(i=3;i>=0;i--)
{
value|=((v1&(1<<i))<<(i+1));
value|=((v2&(1<<i))<<i);
}
return(value);
}

/* 11001 11010 */
/* 11 0011 1010 */
/* 0x03 0x0A */

/* 1010010110 1010011001 */
/* 1010 0101 1010 1001 1001 */
/* 0x0A 0x05 0x0A 0x09 0x09 */

int main(void)
{

unsigned short start=0;

start=0x03;
printf("0x03:%02x 0x5A:%04x\n",start,man_nyb(start));

start=0x0A;
printf("0x0A:%02x 0x99:%04x\n",start,man_nyb(start));

return(0);
}


Rod Pemberton
 
A

Alex Fraser

Jhon said:
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.

There are many ways to do it. For example:

unsigned long manchester_encode(unsigned x, int n) {
/* Assumes 0 < n <= (the number of bits in unsigned), and
* 2n <= (the number of bits in unsigned long).
*
* Returns bits 0,1,...,n-1 of x in bits 1,3,... and the complement in
* bits 0,2,...
*/
unsigned long r = 0;
do
r = (r << 2) | (((x >> --n) & 1) + 1);
while (n);
return r;
}

Alex
 
J

Jhon

Hi guys,
Thanks for posts, Alex what is your point? your code seem quite
sexy but really hard to understand.

I have solved the problem in this 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)
{
PORTB |= (1<<PB3);
PORTB &= ~(1<<PB3);
manchester =1;
}
else //manchester ==1
{
PORTB &= ~(1<<PB3);
PORTB |= (1<<PB3);
manchester =0;
}

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

How about this! isn't sexy as well?
 
J

Jhon

Hmm, just tested the Alex code and BRAVO it is really really sexy and
quite handy.

Alex thanks for that.
 
W

wildfox86

Alex said:
There are many ways to do it. For example:

unsigned long manchester_encode(unsigned x, int n) {
/* Assumes 0 < n <= (the number of bits in unsigned), and
* 2n <= (the number of bits in unsigned long).
*
* Returns bits 0,1,...,n-1 of x in bits 1,3,... and the complement in
* bits 0,2,...
*/
unsigned long r = 0;
do
r = (r << 2) | (((x >> --n) & 1) + 1);
while (n);
return r;
}

Alex

now try these:
http://tinyurl.com/97oxf

haha
 
T

tmp123

Alex said:
There are many ways to do it. For example:

unsigned long manchester_encode(unsigned x, int n) {
/* Assumes 0 < n <= (the number of bits in unsigned), and
* 2n <= (the number of bits in unsigned long).
*
* Returns bits 0,1,...,n-1 of x in bits 1,3,... and the complement in
* bits 0,2,...
*/
unsigned long r = 0;
do
r = (r << 2) | (((x >> --n) & 1) + 1);
while (n);
return r;
}

Alex

A variation (not tested) of same, when the size of the data is
predefined (I assume input 16 bits and output 32 bits and "int" greater
than 32) could be:

/* x input: 16 bits length */
/* r output: 32 bits length */
{
int t;
for ( r=0x55555555, t=3; x; x>>=1, t<<=2 )
if ( x&1 ) r ^= t;
}
 
A

Alex Fraser

Jhon said:
Hi guys,
Thanks for posts, Alex what is your point? your code seem quite
sexy but really hard to understand.

The only tricky thing is realising that adding one to 0 or 1 gives you,
respectively, 01 or 10 in binary. That is, the original value in bit 1, with
its complement in bit 0.
I have solved the problem in this 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)
{
PORTB |= (1<<PB3);
PORTB &= ~(1<<PB3);
manchester =1;
}
else //manchester ==1
{
PORTB &= ~(1<<PB3);
PORTB |= (1<<PB3);
manchester =0;
}

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

The condition in the first if statement is wrong: it will always be false
for bits 9 and 8 (the first two iterations).

The variable 'manchester' is redundant. Setting 'onebit' to zero is also
redundant.

The code appears to be controling a GPIO output of a microcontroller, but
does not appear to care about timing. If the timing is wrong, the output is
not valid Manchester code.

Finally, as a matter of style, the loop could trivially be converted to a
do...while loop.

Alex
 
A

Alex Fraser

tmp123 said:
A variation (not tested) of same, when the size of the data is
predefined (I assume input 16 bits and output 32 bits and "int" greater
than 32) could be:

/* x input: 16 bits length */
/* r output: 32 bits length */
{
int t;
for ( r=0x55555555, t=3; x; x>>=1, t<<=2 )
if ( x&1 ) r ^= t;
}

I haven't tested it either, but it seems quite different... the bits end up
in reverse order, with the position of the Manchester code for the LSB of x
in a position that depends on the highest set bit of x. The latter could be
fixed by running the loop for 16 (or whatever) iterations.

Alex
 
T

tmp123

Alex said:
I haven't tested it either, but it seems quite different... the bits end up
in reverse order, with the position of the Manchester code for the LSB of x
in a position that depends on the highest set bit of x. The latter could be
fixed by running the loop for 16 (or whatever) iterations.

Alex

It seems to work OK (if I understood the problem). When input is 0x033A
(like in the OP):

x=0x33A t=0x3 r=0x55555555
x=0x19D t=0xC r=0x55555555
x=0xCE t=0x30 r=0x55555559
x=0x67 t=0xC0 r=0x55555559
x=0x33 t=0x300 r=0x55555599
x=0x19 t=0xC00 r=0x55555699
x=0xC t=0x3000 r=0x55555A99
x=0x6 t=0xC000 r=0x55555A99
x=0x3 t=0x30000 r=0x55555A99
x=0x1 t=0xC0000 r=0x55565A99
final x=0x0 t=0x300000 r=0x555A5A99
 
A

Alex Fraser

[snip]
It seems to work OK (if I understood the problem). When input is 0x033A
(like in the OP): [snip]
final x=0x0 t=0x300000 r=0x555A5A99

Sorry, you're right. I misunderstood your code.

Alex
 
A

Ali

Hi EveryOne!

Quite interesting discussion here , yeah Alex is right
and Jhon must be having trouble with this method. I remember doing it
for MCU and that really sucks because of less memory , less clock and
every thing with certain limitation;-). Sure a code written for PC can
do this trick without effecting any thing but i can bet that its not
gonna work if timing is an issue in development.

So folks what is the basic idea while working on MCU's? Is it lesser
iterations or less shift operations (either left or right) in code. And
how much do if and switch structures(conditional) can effect the
timing issues.

Regards.
ali
 
K

Keith Thompson

Ali said:
Quite interesting discussion here , yeah Alex is right
and Jhon must be having trouble with this method. I remember doing it
for MCU and that really sucks because of less memory , less clock and
every thing with certain limitation;-). Sure a code written for PC can
do this trick without effecting any thing but i can bet that its not
gonna work if timing is an issue in development.

So folks what is the basic idea while working on MCU's? Is it lesser
iterations or less shift operations (either left or right) in code. And
how much do if and switch structures(conditional) can effect the
timing issues.

Please provide context when you post a followup. Read and understand
<http://cfaj.freeshell.org/google/> before you post here again.
Thanks.

I don't know what an MCU is; www.acronymfinder.com shows 14
definitions just under Information Technology. Don't expect a bunch
of C programming weenies to know about any particular hardware.

The C language doesn't say anything about the relative performance of
various operations; it only specifies what they do. Since you know
what an MCU is, you'll have a better idea than most of us about where
your question might be topical.
 
A

Ali

before you post here again.
Sorry for ruining such a holy thread;-)
I don't know what an MCU is;
Since you know what an MCU is, you'll have a better idea than most of us about where your question might be topical.

Very Strange! Sir the suggestion you have given that i should be
reading the OP before posting is no more valid in your case, yeah , i
wasn't the one who first started the using the term 'MCU'. If you
look at the post of Alex you should find the same term so rather asking
www.acronymfinder.com to show you the results you should be reading
complete thread;-)

The C language doesn't say anything about the relative performance of various operations;

Sorry i don't agree. Don't you think every book of c gives us a
comparative analysis of using the various operation? for example you
can find the difference of inline functions and macro operations in
almost every book.
And same for iteration and recursion.



Cheers!
MCU = Micro Controller Unit
 
K

Keith Thompson

Ali said:
Sorry for ruining such a holy thread;-)

There's no need for sarcasm. And please don't snip attribution lines
(the lines that look like "So-and-so writes:").
Very Strange! Sir the suggestion you have given that i should be
reading the OP before posting is no more valid in your case, yeah , i
wasn't the one who first started the using the term 'MCU'. If you
look at the post of Alex you should find the same term so rather asking
www.acronymfinder.com to show you the results you should be reading
complete thread;-)

Alex mentioned something about a "microcontroller"; you introduced the
term "MCU".
Sorry i don't agree.

Then I'm afraid you're wrong.
Don't you think every book of c gives us a
comparative analysis of using the various operation? for example you
can find the difference of inline functions and macro operations in
almost every book.
And same for iteration and recursion.

The C language is defined by an ISO standard. Find a copy of the
standard (drafts are freely available; google "n1124.pdf") and show us
where it discusses the relative performance of various operations.

The question that you asked was about the relative performance of
certain C constructs on certain specific systems. There is no answer
to your question in the context of the C programming language, which
is what this newsgroup discusses.

If you're still curious, you *might* have better luck in
comp.arch.embedded, but I don't read that newsgroup so I'm only
guessing.
 
A

Ali

The C language is defined by an ISO standard. Find a copy of the standard (drafts are >freely available; google "n1124.pdf") and show us where it discusses the relative >performance of various operations.

Hmm ,did i mention any thing about ISO standard? I told you to see c
books not the draft!
The question that you asked was about the relative performance of certain C >constructs on certain specific systems. There is no answer to your question in the >context of the C programming language, which is what this newsgroup discusses.

I guess i would be asking this to some html group;-)
Ok i was talking about certain systems BUT it was actually about using
'c' on those systems. Sorry i misunderstood that 'c' is a programming
language without any specification of system and running os.
I think there should be an addition of clause in ISO draft that 'c' is
only for certain PC [x86 from Intel ,AMD and no other ]with certain
running OS [32 bit , NT kernel , UNIX , Linux and no other]. ( l0l )

Cheers!
 
F

Flash Gordon

Ali wrote:

Keith requested that you *not* snip the attribution lines. He didn't ask
just for the hell of it.
Hmm ,did i mention any thing about ISO standard? I told you to see c
books not the draft!

The point is that C books do *not* define the language. The ISO standard
does. Therefore, if it is part of the C language it will be in the
standard, if it isn't then it isn't.
I guess i would be asking this to some html group;-)

Not funny. In the text you snipped Keith suggested a perfectly
reasonable group for you to ask.
Ok i was talking about certain systems BUT it was actually about using
'c' on those systems. Sorry i misunderstood that 'c' is a programming
language without any specification of system and running os.
I think there should be an addition of clause in ISO draft that 'c' is
only for certain PC [x86 from Intel ,AMD and no other ]with certain
running OS [32 bit , NT kernel , UNIX , Linux and no other]. ( l0l )

No, the point is it is used on vastly more systems than you are aware
of. We no more want questions specific to those systems than we want
questions specific to whatever system you mentioned before.
--
Flash Gordon
Living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidlines and intro -
http://clc-wiki.net/wiki/Intro_to_clc
 
C

Chris Dollin

Ali said:
Hmm ,did i mention any thing about ISO standard? I told you to see c
books not the draft!

The books don't define the language; the standard does. Performance is a
quality-of-implementation issue.
 
A

Ali

Keith requested that you *not* snip the attribution lines. He didn't ask just for the hell of it.

Then what the heck you are doing if not sniping.
Not funny. In the text you snipped Keith suggested a perfectly reasonable group for you to ask.
I didn't mean that his opinion was outrageous. I appreciate his time
*but* i was really trying to be funny;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top