[b][red]This message was edited by Gregry2 at 2006-9-19 5:28:56[/red][/b][hr] : 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 [code] 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 [/code] not so sure if thats the best, but there {2}rIng
: : 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++.
: : : 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++. : : [blue]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.[/blue]
: : : : 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++. : : : : : [blue]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.[/blue] :
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... :-)
Comments
:
: Plz send me the code to swap two bits of byte data ,
: Waiting for positive reply,
:
: Ex: Before: 0x80
: After : 0x01
:
: Rajesh
:
[code]
int main()
{
int x = 0x80;
x = 0x01;
return 0;
}
[/code]
Enjoy! And don't hesitate to beg for more code!
: 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
[code]
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
[/code]
not so sure if thats the best, but there
{2}rIng
edit: added codetags
:
: 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
: :
: : 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++.
: : :
: : : 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++.
:
:
[blue]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.[/blue]
: : : :
: : : : 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++.
: :
: :
: [blue]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.[/blue]
:
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... :-)
Plesase see the code below and make sure p2 > p1 for swapping bits
int main()
{
int n=32;
int p1,p2;
printf("
Enter positions 1 :");
scanf("%d",&p1);
printf("
Enter positions 2 :");
scanf("%d",&p2);
n = ((n & (1<<p1)) ^ ((n&(1<<p2)) >> (p2-p1)) ) ? ((n ^ (1 << p1)) ^ (1<<p2)):(n) ;
printf("
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
: