CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

bitwise operator operations

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
MCUprogrammer



Joined: 08 Sep 2020
Posts: 221

View user's profile Send private message

bitwise operator operations
PostPosted: Sat May 21, 2022 7:53 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat May 21, 2022 10:44 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat May 21, 2022 11:38 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat May 21, 2022 11:49 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat May 21, 2022 2:05 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat May 21, 2022 2:57 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat May 21, 2022 3:32 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sun May 22, 2022 3:32 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun May 22, 2022 9:47 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun May 22, 2022 10:24 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun May 22, 2022 10:28 am     Reply with quote

initial value = 00000000
_________________
Best Regards...
MCUprogrammer
_______________________________
Work Hard
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun May 22, 2022 10:49 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun May 22, 2022 11:13 am     Reply with quote

thanks sir...
_________________
Best Regards...
MCUprogrammer
_______________________________
Work Hard
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group