Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Meaning of the statement in C++?

Status
Not open for further replies.

areeb

Junior Member level 2
Junior Member level 2
Joined
Jan 28, 2009
Messages
20
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,283
Activity points
1,421
What does the following C++ code do??

Hi guys,

I am new to programming, what will be the output of the following code, please respond step by step how will this code work. Thanks!


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#define reloff PRT1DR &= 0x3a
 
char d=-20;
void main()
{
while(1)
  {
    d++;
    if(d==10)
    {
       d=290;
    }
    if(d>1000)
    {
       d=0;
    }
  }
}

 
Last edited by a moderator:

Re: What does the following C++ code do??

The code above seems more complying with C syntax than with C++.
There is no output declared there, so I'm assuming that variable d is what you are interested.

Moreover, there are some possible inconsistencies, such as declaring a variable as char type ( whose magnitude is 8 bits in most uCs ) but you're attempting to store values greater than 255.
 

Hi, can any please explain what does the following statement mean and what is the purpose? Thanks

Code:
#define reloff PRT1DR &= 0x7f
 
Last edited by a moderator:

PRT1DR = PRT1DR AND 0x7f

it's a bitwise AND operation
(for each bit of PRT1DR and 0x7f)

and #define is making "reloff" work like compile-time function doing that operation
 
  • Like
Reactions: areeb

    areeb

    Points: 2
    Helpful Answer Positive Rating
Ahan, and what is PRT1DR? And reloff?? I am not able to find any reference that explains these terms.
 

reloff in this case is user-defined name - you can use any other valid name you want (as long it's not used somewhere else)

PRT1DR - you need to tell me what is the source of this code and what machine, but it looks like a specific MCU register name...
 
  • Like
Reactions: areeb

    areeb

    Points: 2
    Helpful Answer Positive Rating
I would guess it's a reference to the port direction bits on a Cypress processor.

"PRT1DR &= 0x7f" means the contents of PRT1DR are ANDed with 0x7F and the result put back in PRT1DR, in other words, bit 7 is made zero with other bits unchanged.

"#define reloff PRT1DR &= 0x7f" just makes a new instruction in your program called 'reloff' which the compiler expands to 'PRT1DR &= 0x7' when it sees it.

So 'reloff' makes bit 7 of PRT1DR become zero.

Generally, we use #define like that to make the program more readable, for example you can use it so a port pin becomes synonymous with what is connected to it. So if you used "#define RelayOn PORTX,6=1" you could use the command "RelayOn" in your code rather than an instruction that didn't describe what it actually did. It also makes it a lot easier to modify the code afterwards. Example: if the relay was changed to bit 5 instead of 6 you only have to change the number in the definition and recompile. Without it you would have to go through the entire code and edit all the occurrences from 6 to 5.

Brian.
 
  • Like
Reactions: areeb

    areeb

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top