C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
How to SWAP 2 Bits in a BYTE Data Posted by Rajesh N on 19 Sept 2006 at 4:56 AM
Hi Friends,

Plz send me the code to swap two bits of byte data ,
Waiting for positive reply,

Ex: Before: 0x80
After : 0x01

Rajesh
Report
Re: How to SWAP 2 Bits in a BYTE Data Posted by Lundin on 19 Sept 2006 at 5:19 AM
: Hi Friends,
:
: Plz send me the code to swap two bits of byte data ,
: Waiting for positive reply,
:
: Ex: Before: 0x80
: After : 0x01
:
: Rajesh
:


int main()
{
  int x = 0x80;
  x = 0x01;

  return 0;
}


Enjoy! And don't hesitate to beg for more code!
Report
Re: How to SWAP 2 Bits in a BYTE Data Posted by Gregry2 on 19 Sept 2006 at 5:27 AM
This message was edited by Gregry2 at 2006-9-19 5:28:56

: Hi Friends,
:
: Plz send me the code to swap two bits of byte data ,
: Waiting for positive reply,
:
: Ex: Before: 0x80
: After : 0x01
:
: Rajesh
:

asking for code, sounds like another homework question. I'll give you pseudo code...this is off the top of my head, so it might not be efficient
let 'a' be the number, copy the number into 'b'

make a mask that is zeroes except on the first bit, call it mask1
make another mask that is zeroes except on the other bit, call it mask2

binary and mask1 with 'a', lets call it 'f'
binary and mask2 with 'a', lets call it 's'

if not ((f>0 && s>0) || (f==0 && s==0))
  if f>0 then b = binary or of mask2 with a
  else b= binary or of mask1 with a
return b, or just b is the value

not so sure if thats the best, but there
{2}rIng

edit: added codetags
Report
Re: How to SWAP 2 Bits in a BYTE Data Posted by bilderbikkel on 19 Sept 2006 at 8:12 AM
: Hi Friends,
:
: Plz send me the code to swap two bits of byte data ,
: Waiting for positive reply,
:
: Ex: Before: 0x80
: After : 0x01
:
: Rajesh

Pseudocode:
- Get the byte
- Get the two bits
- Swap the two bits
- Put the bits back in the byte

You can do without bithsifting, but perhaps this page give some inspiration:
http://www.codepedia.com/1/CppBitShifting

See ya and good luck,
bilderbikkel

Report
Re: How to SWAP 2 Bits in a BYTE Data Posted by Lundin on 19 Sept 2006 at 11:41 PM
: : Hi Friends,
: :
: : Plz send me the code to swap two bits of byte data ,
: : Waiting for positive reply,
: :
: : Ex: Before: 0x80
: : After : 0x01
: :
: : Rajesh
:
: Pseudocode:
: - Get the byte
: - Get the two bits
: - Swap the two bits
: - Put the bits back in the byte
:
: You can do without bithsifting, but perhaps this page give some inspiration:
: http://www.codepedia.com/1/CppBitShifting
:
: See ya and good luck,
: bilderbikkel
:
:


I don't see how you could do it without shifting in an effective way.
I would write it in assembler myself, since this is a typical algorithm where assembler is much more efficient - you don't have rotate and check carry in C/C++.

Report
Re: How to SWAP 2 Bits in a BYTE Data Posted by AsmGuru62 on 20 Sept 2006 at 4:08 AM
: : : Hi Friends,
: : :
: : : Plz send me the code to swap two bits of byte data ,
: : : Waiting for positive reply,
: : :
: : : Ex: Before: 0x80
: : : After : 0x01
: : :
: : : Rajesh
: :
: : Pseudocode:
: : - Get the byte
: : - Get the two bits
: : - Swap the two bits
: : - Put the bits back in the byte
: :
: : You can do without bithsifting, but perhaps this page give some inspiration:
: : http://www.codepedia.com/1/CppBitShifting
: :
: : See ya and good luck,
: : bilderbikkel
: :
: :
:
:
: I don't see how you could do it without shifting in an effective way.
: I would write it in assembler myself, since this is a typical algorithm where assembler is much more efficient - you don't have rotate and check carry in C/C++.
:
:
Why check anything? Simply mask out the unneeded bits and then shift both values and then clear the old bit values and then OR them with the values from previoud shift.
Report
Re: How to SWAP 2 Bits in a BYTE Data Posted by Lundin on 20 Sept 2006 at 4:29 AM
: : : : Hi Friends,
: : : :
: : : : Plz send me the code to swap two bits of byte data ,
: : : : Waiting for positive reply,
: : : :
: : : : Ex: Before: 0x80
: : : : After : 0x01
: : : :
: : : : Rajesh
: : :
: : : Pseudocode:
: : : - Get the byte
: : : - Get the two bits
: : : - Swap the two bits
: : : - Put the bits back in the byte
: : :
: : : You can do without bithsifting, but perhaps this page give some inspiration:
: : : http://www.codepedia.com/1/CppBitShifting
: : :
: : : See ya and good luck,
: : : bilderbikkel
: : :
: : :
: :
: :
: : I don't see how you could do it without shifting in an effective way.
: : I would write it in assembler myself, since this is a typical algorithm where assembler is much more efficient - you don't have rotate and check carry in C/C++.
: :
: :
: Why check anything? Simply mask out the unneeded bits and then shift both values and then clear the old bit values and then OR them with the values from previoud shift.
:


I'm assuming he wants a generic "mirror" algorithm, ie 0xAAAA would be translated to 0x5555. Otherwise the source code I first posted would do fine as well...
Report
Re: How to SWAP 2 Bits in a BYTE Data Posted by k_v_ashok11 on 16 Dec 2008 at 10:53 PM
Hi,
Plesase see the code below and make sure p2 > p1 for swapping bits

int main()
{
int n=32;

int p1,p2;

printf("\n Enter positions 1 :");
scanf("%d",&p1);

printf("\n Enter positions 2 :");
scanf("%d",&p2);
n = ((n & (1<<p1)) ^ ((n&(1<<p2)) >> (p2-p1)) ) ? ((n ^ (1 << p1)) ^ (1<<p2)):(n) ;
printf("\n result : %d ", n);
}






: Hi Friends,
:
: Plz send me the code to swap two bits of byte data ,
: Waiting for positive reply,
:
: Ex: Before: 0x80
: After : 0x01
:
: Rajesh
:

Report
This post has been deleted. Posted by Lundin on 17 Dec 2008 at 5:05 AM
This post has been deleted.



 

Recent Jobs