View previous topic :: View next topic |
Author |
Message |
MCUprogrammer
Joined: 08 Sep 2020 Posts: 221
|
bitwise operator operations |
Posted: Sat May 21, 2022 7:53 am |
|
|
Hi everybod;
How bitwise operator operations work with this code I wrote? Can someone explain this with an example? So these hex numbers are written as binary. What are the next steps? Can you help with this written code and explain the logic? Thanks.
Code: |
1.) value = input_e() & 0xDF;
2.) void func(unsigned int8 value)
{
unsigned int8 output_value;
output_value = input_c() & 0xF8; //read PORTC, clear C0, C1, C2 and preserve C3 to C7
output_value |= (value & 0x07); //update only C0, C1, C2
output_c(output_value); //output to PORTC
}
|
_________________ Best Regards...
MCUprogrammer
_______________________________
Work Hard |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Sat May 21, 2022 10:44 am |
|
|
Forget about binary hex etc.
All values inside the chip are binary. Decimal, hex etc., are just
'representations'. Other ways of writing the values.
Code: |
value = input_e() & 0xDF;
//you don't show where this is used, but 0xDF is 1101 1111
//value will have ii0iiiii where 'i' is a bit from the input
void func(unsigned int8 value)
{
unsigned int8 output_value;
output_value = input_c() & 0xF8; //read PORTC, clear C0, C1, C2 and preserve C3 to C7
//here output_value will be iiiii000
output_value |= (value & 0x07); //update only C0, C1, C2
//the bracketted part gives 00000vvv where v is the bit from value
//Then the or give output_value will now be iiiiivvv
output_c(output_value); //output to PORTC
}
|
& gives:
bit bit output
0 0 0
0 1 0
1 0 0
1 1 1
| gives
bit bit output
0 0 0
0 1 1
1 0 1
1 1 1 |
|
|
PrinceNai
Joined: 31 Oct 2016 Posts: 479 Location: Montenegro
|
|
Posted: Sat May 21, 2022 11:38 am |
|
|
AND gives "1" ONLY when both corresponding bits are "1" and it is used to clear some bits
So if your variable is:
11011111 and you want to clear last 3 bits, you AND it with
11111000 and you get
-----------
11011000 as a result. Only the last 3 bits changed, others are as they were
OR on the other hand gives "1" when AT LEAST ONE BIT is "1" and you can use it to set some bits in your variable.
10100000 and you want to set last 3 bits you or it with
00000111 and you get
-----------
10100111 as a result |
|
|
PrinceNai
Joined: 31 Oct 2016 Posts: 479 Location: Montenegro
|
|
Posted: Sat May 21, 2022 11:49 am |
|
|
As Ttelmah said, all the variables live as binary inside the PIC. When writing a program, you can use whatever notation you want, bin, hex, dec. At least for me, when doing any kind of bit manipulation with those operators, binary is the easiest because you can see the result directly. |
|
|
MCUprogrammer
Joined: 08 Sep 2020 Posts: 221
|
|
Posted: Sat May 21, 2022 2:05 pm |
|
|
input_e() 7 button inputs;
& bitwise operator
0xDF = 11011111
input_e() = 00000101 information came In this case, value variable result
The value 00000101 = 5 occurs.
is it correct?
-------------------------------------------------------------------------------
The value 9 is passed as an argument to the func(9) function.
/* my understanding for this code;
output_value = input_c() & 0xF8; //read PORTC, clear C0, C1, C2 and preserve C3 to C7 */
He studied PORTC. Send '0' to first 3 bits of 8 bits and '1' to all subsequent bits to clear C0-C1-C2. At first when input_c() reads all pins are 0.
0xF8 = 11111000
input_c()= 00000000
output_value = 00000000. Is it true ?
/* what I understand for this code
output_value |= (value & 0x07); //update only C0, C1, C2
*/
output_value |= (0000101 & 00000111); func(5) was sent.
When I put (value & 0x07) into bitwise processing, the result = 00000101
what happens next? How to proceed next. And how does the value of 5 go to the output portc and light the output leds.
In addition, only transactions related to related entries &,| How do you decide to use it?
So for example to update | bitwise operator, am I using the & operator to preserve the value of other I/Os? _________________ Best Regards...
MCUprogrammer
_______________________________
Work Hard |
|
|
PrinceNai
Joined: 31 Oct 2016 Posts: 479 Location: Montenegro
|
|
Posted: Sat May 21, 2022 2:57 pm |
|
|
The results seem correct for your values. Once again, when using AND you can only set (or update, as you put it) some bits of the variable to 0. Never to 1. Unchanged or 0.
When using OR, you can set some bits to 1 and never to 0. Unchanged or 1.
So if you want to force some bits to zero, use AND.
If you want to force some bits to one, use OR.
You have loads of bitwise calculators on the web to try and train with some numbers and check the results.
As for outputting the result, output_c(output_value); should do it. |
|
|
MCUprogrammer
Joined: 08 Sep 2020 Posts: 221
|
|
Posted: Sat May 21, 2022 3:32 pm |
|
|
output_value |= (value & 0x07); ok I said for this code. so actually, doesn't that mean the following code? How is the expansion done here?
output_value = output_value | (value & 0x07); _________________ Best Regards...
MCUprogrammer
_______________________________
Work Hard |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun May 22, 2022 3:32 am |
|
|
You want to know if two lines of C code produce the same result.
You can investigate questions like this by yourself.
Make a test program to do this. Example:
Code: |
#include <18F46K22.h>
#fuses NOWDT
#use delay(internal=4M)
//=======================================
void main()
{
int8 value;
int8 output_value;
output_value |= (value & 0x07);
output_value = output_value | (value & 0x07);
while(TRUE);
}
|
Compile it. Then look at the .LST file:
Code: |
.... output_value |= (value & 0x07);
00022: MOVF value,W
00024: ANDLW 07
00026: IORWF output_value,F
....
.... output_value = output_value | (value & 0x07);
00028: MOVF value,W
0002A: ANDLW 07
0002C: IORWF output_value,F
|
The .LST file shows that both methods produce identical code.
You can use this method in the future to answer some questions by yourself. |
|
|
MCUprogrammer
Joined: 08 Sep 2020 Posts: 221
|
|
Posted: Sun May 22, 2022 9:47 am |
|
|
Ok, I know they both produce the same code. I have explained above. What I want to tell. First, it did the bitwise operation inside the parentheses and produced a single binary code. then how it handles it with output_value. For example;
output_value = output_value | (value & 0x07);
first of the above code (value & 0x07); processed here. (00000101 & 00000111) bitwise result of operation 00000101
later
what is the value of output_value here.
output_value = output_value | 00000101 bitwise operation is done here and what is the output_value _________________ Best Regards...
MCUprogrammer
_______________________________
Work Hard |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun May 22, 2022 10:24 am |
|
|
What is the initial value of output_value ? We need to know that, before
we can tell what the result is. |
|
|
MCUprogrammer
Joined: 08 Sep 2020 Posts: 221
|
|
Posted: Sun May 22, 2022 10:28 am |
|
|
initial value = 00000000 _________________ Best Regards...
MCUprogrammer
_______________________________
Work Hard |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun May 22, 2022 10:49 am |
|
|
Well it's very simple:
0 | 00000101 gives 00000101 as the result.
This is something you should learn from a book on the C language. |
|
|
MCUprogrammer
Joined: 08 Sep 2020 Posts: 221
|
|
Posted: Sun May 22, 2022 11:13 am |
|
|
thanks sir... _________________ Best Regards...
MCUprogrammer
_______________________________
Work Hard |
|
|
|