472,779 Members | 2,524 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,779 software developers and data experts.

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 2876
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
news:11**********************@g43g2000cwa.googlegr oups.com...
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.

Now separate every 2 bits by masking (masks 00110011... and 11001100...
) and shift the latter by two to the right. Add again.

Now separate every 4 bits by masking (masks 00001111.... and
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;
}

How about this! isn't sexy as well?

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Michael Foord | last post by:
#!/usr/bin/python -u # 15-09-04 # v1.0.0 # auth_example.py # A simple script manually demonstrating basic authentication. # Copyright Michael Foord # Free to use, modify and relicense. #...
7
by: jesse | last post by:
In java, one constructor can call another constructor through this(...) for instance class foo { public: foo(int k) { this(k,false)}; foo(int k, boolean m){...}; }
4
by: LCAdeveloper | last post by:
I have had to move to Visual Studio.NET Pro. from Visual Basic 4.0 and am now starting to re-write our code. I was a bit surprised to find that Visual Basic.NET no longer supports fixed length...
1
by: Scott Haner via DotNetMonster.com | last post by:
Hey; I've done a decent amount of VB.net in University, but the next stage for a major project we have been asked to use 'modulization' and that the judges want to see as much code and functions...
7
by: Lau Lei Cheong | last post by:
Hello, Actually I think I should have had asked it long before, but somehow I haven't. Here's the scenerio: Say we have a few pages in an ASP.NET project, each of them needs to connect to...
25
by: Jhon | last post by:
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...
2
by: Key9 | last post by:
Question about basic input output. HI All I am a new C++er Suppose I am designing a i/o device . system have screens . system have keyboards.
3
by: masood.iqbal | last post by:
In all the sample code snippets of try-catch code blocks that I have seen, the catch block does one of the following three things: 1). exits the program (after spitting out a cerr message) 2). ...
3
by: richie9648 | last post by:
Hi All I have created a macro which turns reports done by users into a format that is needed by me. The good this is that all the reports are in the same format. However my question is around...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.