Aug 23, 2007 #1 K 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!
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!
Aug 24, 2007 #2 B brmadhukar Advanced Member level 3 Joined Jun 21, 2002 Messages 839 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
Aug 24, 2007 #3 J jhbbunch Full Member level 4 Joined Feb 21, 2006 Messages 197 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; }
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; }
Aug 25, 2007 #4 D 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 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
Aug 25, 2007 #5 K 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.
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.
Aug 25, 2007 #6 E 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; }
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; }
Aug 25, 2007 #7 VSMVDD Banned Joined Jun 12, 2005 Messages 643 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
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
Aug 25, 2007 #8 leomecma Full Member level 5 Joined Jun 17, 2005 Messages 244 Helped 14 Reputation 28 Reaction score 1 Trophy points 1,298 Location Brasil Activity points 3,933 Re: How to write a C program which exchange the bits in a by Go to the title Reversing bit sequences https://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious Maybe you can get a good idea ... leomecma
Re: How to write a C program which exchange the bits in a by Go to the title Reversing bit sequences https://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious Maybe you can get a good idea ... leomecma
Aug 26, 2007 #9 W 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.
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.
Aug 26, 2007 #10 M 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.
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.
Aug 27, 2007 #11 W 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)