Bitwise operations

General discussion on mikroC.
Post Reply
Author
Message
murta
Posts: 41
Joined: 16 May 2005 23:39
Location: Sweden
Contact:

Bitwise operations

#1 Post by murta » 02 Jun 2005 20:00

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. :?

LGR
Posts: 3204
Joined: 23 Sep 2004 20:07

#2 Post by LGR » 02 Jun 2005 21:46

dsPIC has an instruction for that. :P

The simplest thing to do is use a constant array as a lookup table.
If you know what you're doing, you're not learning anything.

murta
Posts: 41
Joined: 16 May 2005 23:39
Location: Sweden
Contact:

#3 Post by murta » 02 Jun 2005 22:42

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? :?: :?

LGR
Posts: 3204
Joined: 23 Sep 2004 20:07

#4 Post by LGR » 02 Jun 2005 23:14

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:

Code: Select all

const table : array[256] of byte = (0,128,64,192,32, .....)
I know that there is something similar in C. Constants will be stored in ROM.
If you know what you're doing, you're not learning anything.

gambrose
Posts: 369
Joined: 28 Sep 2004 17:34
Location: uk

#5 Post by gambrose » 03 Jun 2005 10:27

you could use RLF or RLCF to push a bit off the value into the carry bit then RRF or RRCF it into a new value do this 8 times and your done.
Graham Ambrose

murta
Posts: 41
Joined: 16 May 2005 23:39
Location: Sweden
Contact:

#6 Post by murta » 03 Jun 2005 19:40

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 ??

User avatar
zristic
mikroElektronika team
Posts: 6608
Joined: 03 Aug 2004 12:59
Contact:

#7 Post by zristic » 03 Jun 2005 20:54

Try this (Pascal):

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.
Not that fast as assembly, but much easier to implement.
Last edited by zristic on 03 Jun 2005 20:55, edited 1 time in total.

LGR
Posts: 3204
Joined: 23 Sep 2004 20:07

#8 Post by LGR » 03 Jun 2005 20:54

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,

Code: Select all

Q.0 = V.7
Q.1 = V.6
Q.2 = V.5
.
.
Q.7 = V.0
(I hope that is correct C)

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.

User avatar
zristic
mikroElektronika team
Posts: 6608
Joined: 03 Aug 2004 12:59
Contact:

#9 Post by zristic » 03 Jun 2005 20:58

LGR wrote:

Code: Select all

Q.0 = V.7
Q.1 = V.6
Q.2 = V.5
.
.
Q.7 = V.0
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..

Zed
Posts: 41
Joined: 20 Apr 2005 08:47
Location: UK

#10 Post by Zed » 03 Jun 2005 22:16

You mean like this?

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;
}
This works and takes fewer cycles than the solution I was about to post :lol: .

gambrose
Posts: 369
Joined: 28 Sep 2004 17:34
Location: uk

#11 Post by gambrose » 03 Jun 2005 23:01

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
}
This is for P16 for P18 use RLCF and RRCF

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

User avatar
zristic
mikroElektronika team
Posts: 6608
Joined: 03 Aug 2004 12:59
Contact:

#12 Post by zristic » 04 Jun 2005 08:36

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.

gambrose
Posts: 369
Joined: 28 Sep 2004 17:34
Location: uk

#13 Post by gambrose » 04 Jun 2005 10:03

zristic is right asm is not a write and forget thing.

I did request a built in rotate left/right through carry function, as it is quite a useful little instruction. anyway we live in hope :wink:
Graham Ambrose

murta
Posts: 41
Joined: 16 May 2005 23:39
Location: Sweden
Contact:

#14 Post by murta » 04 Jun 2005 15:03

Thanks for all the help, now it works!!!

Post Reply

Return to “mikroC General”