Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

How to write a C program which exchange the bits in a byte?

Status
Not open for further replies.

kukurigu

Junior Member level 3
Joined
Dec 14, 2002
Messages
25
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
272
c program to exchange the nibbles

How to write a C program which exchange the bits in a byte (i.e. mirror the bits)?
There are different approaches but I am looking for the fast one!
 

brmadhukar

Advanced Member level 3
Joined
Jun 21, 2002
Messages
840
Helped
42
Reputation
84
Reaction score
11
Trophy points
1,298
Location
India
Activity points
6,783
Re: How to write a C program which exchange the bits in a by

use a look up table
 

jhbbunch

Full Member level 4
Joined
Feb 21, 2006
Messages
199
Helped
17
Reputation
34
Reaction score
3
Trophy points
1,298
Activity points
2,902
I haven't tested this but the geneal idea is right. Should work.


int MirrorInt(value)
{

int lastbit = 15;
int i;

for ( i = 0; i <= lastbit; i++)
(
if ( 1 << i && value)
value = value || 1 << (lastbit - i );
else
value = value && !(1 <<(lastbit - i ));


)

return value;

}
 

devendra_devgupta

Member level 3
Joined
Apr 8, 2007
Messages
54
Helped
3
Reputation
6
Reaction score
0
Trophy points
1,286
Activity points
1,632
Kukurigu what you are trying to ask. Do you want to exchange nibbles in a byte or tou want to reverse the number in a byte i.e LSB will be MSB and vice versa

Moreover jhbbunch your program seems to be wrong plz check it out
 

kukurigu

Junior Member level 3
Joined
Dec 14, 2002
Messages
25
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
272
Re: How to write a C program which exchange the bits in a by

I want to reverse the bits (i.e mirror) bit7 <-> bit0, bit6 <-> bit1 and so on.
 

Encrypted

Advanced Member level 4
Joined
May 28, 2001
Messages
115
Helped
17
Reputation
36
Reaction score
5
Trophy points
1,298
Activity points
705
Re: How to write a C program which exchange the bits in a by

Not my idea!

Code:
char reverse (char n) {
  n = n & 0b11110000 >> 4 | n & 0b00001111 << 4;  // efghabcd
  n = n & 0b11001100 >> 2 | n & 0b00110011 << 2;  // ghefcdab 
  n = n & 0b10101010 >> 1 | n & 0b01010101 << 1;  // hgfedcba
  return n;
}
 

VSMVDD

Banned
Joined
Jun 12, 2005
Messages
646
Helped
92
Reputation
184
Reaction score
34
Trophy points
1,308
Location
scotland uk
Activity points
0
Re: How to write a C program which exchange the bits in a by

assign the whole port bits 0-7 as a word
the use modulus

portword= ~portword

this will invert the word then you can shift out what bit you want

bit=(BOOL) (portword<<x) // where x is the bit you want
 

whatcall`

Newbie level 5
Joined
Aug 26, 2007
Messages
8
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,281
Activity points
1,339
Re: How to write a C program which exchange the bits in a by

If what you want is just fast may be look-up-table is a better method. But I think for 8-bits byte we can code in a "stupid" way like this:

void reverse_byte(byte &a)
{
byte b ;

b = *a;
*a=0;
*a += (b&(1<<7))>>7;
*a += (b&(1<<6))>>5;
*a += (b&(1<<5))>>3;
*a += (b&(1<<4))>>1;
*a += (b&(1<<3))<<1;
*a += (b&(1<<2))<<3;
*a += (b&(1<<1))<<5;
*a += (b&(1<<0))<<7;

return;
}

I hope this can work and the compiler can optimize it to simple asm instructions.
 

Mr.MEB

Member level 3
Joined
Feb 1, 2007
Messages
54
Helped
6
Reputation
12
Reaction score
2
Trophy points
1,288
Activity points
1,580
void reverse_byte(byte &a)
isn't this some C++ with reference arg?
I don't think it will work with a non-AnsI C compiler for 8 bit mcu.
 

whatcall`

Newbie level 5
Joined
Aug 26, 2007
Messages
8
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,281
Activity points
1,339
Re: How to write a C program which exchange the bits in a by

Sorry.
It should be (byte *a)
 

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top