Traffic Lights - Complete the code.

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4

47 Replies - 29337 Views - Last Post: 10 April 2013 - 07:47 PM

#31 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,298
  • Joined: 22-October 08

Re: Traffic Lights - Complete the code.

Posted 05 March 2012 - 06:02 PM

Thank you :clap:

I've looked over the code and there are a few lines of code that I don't understand.

1.
char lightsBuf[MAXBUF] = {0};

	{
		strcpy((char*)&lightsBuf, (char*)&trafficLightFrame);
		ConfigureLightsAndDisplay(lightsBuf, 0);
	}



Why are there '{ }' is this part of the array? And why is it after the ';'?

2.

Why is the & sign next to the = and whats this ~ doing there?

systemStatus &= ~SHIFTLEFT(2);



Other than that, I've looked over it and it's a great learning curve. I thought I was good, but this challenge has brought me to realize that I am not as good as I had hoped ;)

The timerelapsed method is sooo simple... I was trying to make it so hard :\

Also, I just noticed that I renamed LightsTimer.c to LightsTimer.cpp thinking it was another error you had posted ;)

Thanks for the challenge , I'm really going to practice for the next challenge, and hopefully I will get better at them :)
Was This Post Helpful? 0
  • +
  • -

#32 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Traffic Lights - Complete the code.

Posted 05 March 2012 - 06:26 PM

Glad you like the code aaron1178! :)

1. The braces just reduce scope and can safely be removed. I left them in just to make the code a little clearer. Look at the following code snippet for an explanation.

char lightsBuf[MAXBUF] = {0};

	{
                // Declare a variable in this scope.
                int testVar;

		strcpy((char*)&lightsBuf, (char*)&trafficLightFrame);
		ConfigureLightsAndDisplay(lightsBuf, 0);
	}

        testVar++; // Error, undeclared variable 'testVar' (because it is being used outside the scope of its declaration).



Using such a method is great for restricting scope without having to call another function, for instance, as well as keeping code inline.

2. I'm using bitwise operations there, in that I'm storing the state of the entire system in one byte rather than a series of bools. What the operation does is:

- In binary make the 2^2 (which equals '4') zero by using the bitwise 'not' operator (~) and 'ANDing' that position in the systemStatus byte with zero will clear it to zero. Now store this back to the variable itself.

Hope you enjoy the next challenge! :)
Was This Post Helpful? 1
  • +
  • -

#33 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Traffic Lights - Complete the code.

Posted 05 March 2012 - 06:44 PM

View Postaaron1178, on 06 March 2012 - 01:02 AM, said:

Other than that, I've looked over it and it's a great learning curve. I thought I was good, but this challenge has brought me to realize that I am not as good as I had hoped ;)

Yeah, it takes a lot of practice, but you get there in the end. It helps a great deal when you start out seeing how excellent programmers write code.

View Postaaron1178, on 06 March 2012 - 01:02 AM, said:

The timerelapsed method is sooo simple... I was trying to make it so hard :\

A lot of problems in games are actually very simple if you give it some thought from first principles.

View Postaaron1178, on 06 March 2012 - 01:02 AM, said:

Also, I just noticed that I renamed LightsTimer.c to LightsTimer.cpp thinking it was another error you had posted ;)

Yes, you should/could have looked at the header files referenced in the source file to see that they were C ones instead of C++. extern "C" enforces 'C binding'.

View Postaaron1178, on 06 March 2012 - 01:02 AM, said:

Thanks for the challenge , I'm really going to practice for the next challenge, and hopefully I will get better at them :)

No problem, aaron1178. Try to understand the code as best you can and fire any questions you have. :)
Was This Post Helpful? 0
  • +
  • -

#34 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,298
  • Joined: 22-October 08

Re: Traffic Lights - Complete the code.

Posted 24 March 2013 - 11:56 PM

I hope you don't mind, but I am going to attempt this challenge again. I've spent 3 whole days learning Bitwise and Logic operations I've downloaded truth tables and heaps more info and stuck/wrote into my A4 Subject bible. I've even read some more on binary, like binary addition, subtraction ect in order to understand Left Shifts, AND, OR, XOR. So hopefully I will be able to complete this challenge without snooping at the completed code :)

One question though: Could you upload the incomplete source code to git? Maybe on a separate branch "DICTrafficLights-IncompleteCode" or something? if you're not to busy skiing in the alpines :)

Also, I think it's genius that you store the System Status in a unsigned char( 1 Byte, 8 bits ). This would also mean faster calculations am I correct?

Thanks,

Aaron1178
Was This Post Helpful? 0
  • +
  • -

#35 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Traffic Lights - Complete the code.

Posted 25 March 2013 - 11:55 AM

View Postaaron1178, on 25 March 2013 - 03:56 AM, said:

I hope you don't mind, but I am going to attempt this challenge again. I've spent 3 whole days learning Bitwise and Logic operations I've downloaded truth tables and heaps more info and stuck/wrote into my A4 Subject bible. I've even read some more on binary, like binary addition, subtraction ect in order to understand Left Shifts, AND, OR, XOR. So hopefully I will be able to complete this challenge without snooping at the completed code :)

Not at all! Go for it. :)

View Postaaron1178, on 25 March 2013 - 03:56 AM, said:

One question though: Could you upload the incomplete source code to git? Maybe on a separate branch "DICTrafficLights-IncompleteCode" or something? if you're not to busy skiing in the alpines :)


This is the incomplete code.

View Postaaron1178, on 25 March 2013 - 03:56 AM, said:

Also, I think it's genius that you store the System Status in a unsigned char( 1 Byte, 8 bits ). This would also mean faster calculations am I correct?

Several reasons for this:

1. It's small.
2. Because it's a byte it fits neatly in memory across all platforms; no struct for each red, green or amber light, etc.
3. Bit masking and shifts are as quick as you can get!

Good luck and have fun! :)
Was This Post Helpful? 0
  • +
  • -

#36 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,298
  • Joined: 22-October 08

Re: Traffic Lights - Complete the code.

Posted 25 March 2013 - 05:18 PM

This is just a theoretical question but say we have a file format we just created, if we can use bit manipulation on certain processes and functions, it would increase the speed of said function?
Was This Post Helpful? 0
  • +
  • -

#37 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,298
  • Joined: 22-October 08

Re: Traffic Lights - Complete the code.

Posted 29 March 2013 - 03:10 PM

,

systemStatus ^= systemStatus

Now I understand the concepts and how XOR works, I am just having trouble understanding the above, seings as systemStatus has no values assigned. For instance:

unsigned char myChar;
dec2Bin(myChar);



I'll receive an error that myChar has not been initialized.

Now if I go,

unsigned char myChar;
myChar ^= myChar; //Or myChar = myChar ^ myChar
dec2Bin(myChar);



I will get a blank screen, that nothing is being stored on bit level, yet it's not 0000 0000.

I am confused here :)

Thanks,
Was This Post Helpful? 0
  • +
  • -

#38 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Traffic Lights - Complete the code.

Posted 29 March 2013 - 03:23 PM

Hey aaron1178,

Why do you have dec2bin()? All methods and variables are there and there is no need to add anything apart from the logic. :)

Also, when you XOR something with itself it clears to zero.

This post has been edited by ButchDean: 29 March 2013 - 03:27 PM

Was This Post Helpful? 0
  • +
  • -

#39 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,298
  • Joined: 22-October 08

Re: Traffic Lights - Complete the code.

Posted 29 March 2013 - 07:30 PM

It's not in that solution, it's in my learnings solution where I put everything I learn to the test :)

In regards to the XOR, I understand now that it clears to zero when its XOR with itself, but even if that variable is not initialized?
Was This Post Helpful? 0
  • +
  • -

#40 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Traffic Lights - Complete the code.

Posted 29 March 2013 - 08:54 PM

That is initializing the variable:

a ^= a;

is the same as:

a = 0;

so yes.
Was This Post Helpful? 1
  • +
  • -

#41 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,298
  • Joined: 22-October 08

Re: Traffic Lights - Complete the code.

Posted 31 March 2013 - 06:42 PM

Hey , I've completed the challenge :) Successfully this time hahah. Github:

https://github.com/A.../Traffic-Lights

I had a little trouble with LightsCycled(), but once I got that sorted out, I flew past the rest. Also, I altered the way the Traffic lights work, because here in Australia they go (Red, Green, Amber, Red ect). So I altered the system based on the Australian light system :)
Was This Post Helpful? 1
  • +
  • -

#42 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Traffic Lights - Complete the code.

Posted 01 April 2013 - 07:26 AM

Nice. I haven't tested it because my Windows partition is broken, but nice job in at least breaking down the code to be able to understand it. :)
Was This Post Helpful? 0
  • +
  • -

#43 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,298
  • Joined: 22-October 08

Re: Traffic Lights - Complete the code.

Posted 01 April 2013 - 03:22 PM

Thanks, it's been a tough challenge, though once you know and understand how bitfields work, you fly through it :)

Oh no, R.I.P your Windows partition :P What are you running now? Linux or Mac
Was This Post Helpful? 0
  • +
  • -

#44 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Traffic Lights - Complete the code.

Posted 01 April 2013 - 03:41 PM

Debian Linux, much prefer that tbh. As well as needing it for my current work. :)
Was This Post Helpful? 0
  • +
  • -

#45 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,298
  • Joined: 22-October 08

Re: Traffic Lights - Complete the code.

Posted 01 April 2013 - 04:03 PM

Ah nice :)
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4