Bitwise operations
Bitwise operations
Hello...
Does anyone know a way to flip all the bits in a 8-bit varible?
example:
from this....
LSB-> 11100101 <-MSB
to this...
MSB-> 10100111 <-LSB
Is there a simple way of doing this or?? ... I´m thinking maybe a loop or something.
Does anyone know a way to flip all the bits in a 8-bit varible?
example:
from this....
LSB-> 11100101 <-MSB
to this...
MSB-> 10100111 <-LSB
Is there a simple way of doing this or?? ... I´m thinking maybe a loop or something.
ok..
I´m not sure I know exactly what you mean.
In a previous post I´ve made about PSX controller interfacing I get the value from the analog sticks in 8-bit word format. That is full left = 0xFF and full right = 0x00. I store this value in a unsigned short varible.
Maybe you could tell me how to do this thing whit a static loockup table?
I´m not sure I know exactly what you mean.
In a previous post I´ve made about PSX controller interfacing I get the value from the analog sticks in 8-bit word format. That is full left = 0xFF and full right = 0x00. I store this value in a unsigned short varible.
Maybe you could tell me how to do this thing whit a static loockup table?
A lookup table is just a constant array. I use Pascal, so I'm not exactly sure of the C syntax, but in Pascal it is like:
I know that there is something similar in C. Constants will be stored in ROM.
Code: Select all
const table : array[256] of byte = (0,128,64,192,32, .....)
If you know what you're doing, you're not learning anything.
ok... Now I have two ways of doing this. I though a little about the sollution with an array, but wont it be time consuming to do an array with 256 elements in??
The other aproach with the RLF and RRF seems a little bit more simple to do, but then I have to write assemble language and I don´t know how to do this. Could you show me ??
The other aproach with the RLF and RRF seems a little bit more simple to do, but then I have to write assemble language and I don´t know how to do this. Could you show me ??
Try this (Pascal):
Not that fast as assembly, but much easier to implement.
Code: Select all
function Mirror(Source: byte): byte;
begin
Result := 0;
if Source.0 = 1 then
Result.7 := 1;
if Source.1 = 1 then
Result.6 := 1;
if Source.2 = 1 then
Result.5 := 1;
if Source.3 = 1 then
Result.4 := 1;
if Source.4 = 1 then
Result.3 := 1;
if Source.5 = 1 then
Result.2 := 1;
if Source.6 = 1 then
Result.1 := 1;
if Source.7 = 1 then
Result.0 := 1;
end;
var aa: byte;
// main program
begin
aa := %11000001;
aa := Mirror(aa); // aa = %10000011
nop;
end.
Last edited by zristic on 03 Jun 2005 20:55, edited 1 time in total.
Time consuming to create the text for the array; (you could use Excel, and cut and paste). Actual execution is extremely fast, because the address is computed.
An alternate way may be better. Create a variable called Q, and then if the original variable is V,(I hope that is correct C)
You could do this in a loop; i.e. Q.I = V.(7-I)
An alternate way may be better. Create a variable called Q, and then if the original variable is V,
Code: Select all
Q.0 = V.7
Q.1 = V.6
Q.2 = V.5
.
.
Q.7 = V.0
You could do this in a loop; i.e. Q.I = V.(7-I)
If you know what you're doing, you're not learning anything.
I have just tried this in mikroPascal, with the intention to post the code here, but it did not pass. Compiler reported the argument is out of range. So I did it with ifs..LGR wrote:Code: Select all
Q.0 = V.7 Q.1 = V.6 Q.2 = V.5 . . Q.7 = V.0
You mean like this?
This works and takes fewer cycles than the solution I was about to post .
Code: Select all
void main()
{
char x, source, result;
result.F7 = source.F0;
result.F6 = source.F1;
result.F5 = source.F2;
result.F4 = source.F3;
result.F3 = source.F4;
result.F2 = source.F5;
result.F1 = source.F6;
result.F0 = source.F7;
}
Code: Select all
char Flip(char number)
{
asm RLF Flip_argh0, F
asm RRF _Flip_local_result, F
asm RLF Flip_argh0, F
asm RRF _Flip_local_result, F
asm RLF Flip_argh0, F
asm RRF _Flip_local_result, F
asm RLF Flip_argh0, F
asm RRF _Flip_local_result, F
asm RLF Flip_argh0, F
asm RRF _Flip_local_result, F
asm RLF Flip_argh0, F
asm RRF _Flip_local_result, F
asm RLF Flip_argh0, F
asm RRF _Flip_local_result, F
asm RLF Flip_argh0, F
asm RRF _Flip_local_result, F
}
Note that there is no code for setting the correct bank, mikroC tends t set the bank at the start of every function. I think adding something like char temp = 0; will cover this but may not be needed as bank will have had to be set to set the parameter to start with so I am really not sure what to advise.
Graham Ambrose
The linker leaves users' ASM intact. Therefore, user has to set the bank in ASM himself. There is a way to set the bank before the ASM, by calling the ASM variable just before the ASM block. The compiler will then set the bank before the ASM block and everything will be fine when ASM block excecutes.
However, in the case above, if Flip_argh0 and _Flip_local_result are in different banks then nothing will help you, but to set banks manually.
Therefore, use one of the solutions withuot ASM.
However, in the case above, if Flip_argh0 and _Flip_local_result are in different banks then nothing will help you, but to set banks manually.
Therefore, use one of the solutions withuot ASM.