473,396 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

JK Flip Flop Fun

TMS
119 100+
I'm writing a couple of classes one for a D Flip Flop and one for a JK Flip Flop in C++. The D Flip Flop seems to be working fine, but I'm having issues with the JK Flip Flop.

I'm getting garbage for the variables when passing from one function to the next. Here is my code for the JK Flip Flop header, class and test:
Expand|Select|Wrap|Line Numbers
  1. #ifndef JKflipFlop_h
  2. #define JKflipFlop_h
  3. #include <iostream>
  4. class JKFlipFlop
  5. {
  6. public:
  7. JKFlipFlop();
  8. JKFlipFlop(int, int, int, int, int);
  9. void setJandK(int, int, int, int, int);
  10. int JKFlip(int ipJ, int ipK, int ipQ);
  11. int enableJK(int inputWire, int ipJ, int ipK, int ipQ);
  12. ~JKFlipFlop();
  13. private:
  14. int ipJ;
  15. int ipK;
  16. int ipQ;
  17. int ipWireJ;
  18. int ipWireK;
  19. };
  20. #endif
  21.  
  22. #include "JKflipFlop.h"
  23. #include <iostream>
  24. using namespace std;
  25.  
  26. JKFlipFlop::JKFlipFlop()
  27. {
  28. }
  29. /*
  30. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  31.      constructor for JKfipFlop()
  32. make a copy of J, K and Q for manipulation.
  33. ipJ = J, ipK = K, ipQ= Q, ipWire = inputWire
  34. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  35. */
  36. JKFlipFlop::JKFlipFlop(int J, int K, int Q, int inputWireA, int inputWireB)
  37. {
  38. setJandK(J, K, Q, inputWireA, inputWireB);
  39. }
  40.  
  41. /*
  42. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  43.      function to set ipK, ipK and ipQ
  44. make a copy of J,K and Q for manipulation.
  45. ipJ = J, ipK = K, ipQ = Q, ipWire = inputWire
  46. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  47. */ 
  48. void JKFlipFlop::setJandK(int J, int K, int Q, int inputWireA, int inputWireB)
  49. {
  50. ipJ = J;
  51. ipK = K;
  52. ipQ = Q;
  53. ipWireJ = inputWireA;
  54. ipWireK = inputWireB;
  55. }
  56. /*
  57. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  58.      Enable JK is called to start the flipFlop function.
  59. If the inputWire is low, JKFlipFlop is not called and Q retains the ame state.
  60. If the inputWire is High, JKFlipFlop is called and the wires will be processed.
  61. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  62. */
  63. int JKFlipFlop::enableJK(int ipWireA, int ipJ, int ipK, int ipQ)
  64. {
  65. if(ipWireA == 0)
  66. {
  67.  
  68. return ipQ;
  69. }
  70. else
  71. {
  72.  
  73. return JKFlip(ipWireJ, ipWireK, ipQ);
  74. }
  75.  
  76. }
  77. /*
  78. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  79.      JK flip:
  80. -if enable is low, Q or Q' will not be changed, and therefore
  81. the JKFlipFlop function will not be called.
  82. -if enable is high && J and K are low Q and Q' will not be changed.
  83. -                     J is low, K is high Q is set low and Q' is high
  84. -        J is high and K is low, Q is set to high, Q' is low
  85. -                     J is high and K is high, determine the value of Q and 
  86.                             make it opposite.
  87. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  88. */
  89. int JKFlipFlop::JKFlip(int ipJ, int ipK, int ipQ)
  90. {
  91. cout << "ipJ = " << ipJ;
  92. cout << ", ipK = " << ipK ;
  93. cout << ", ipQ = " << ipQ << endl;
  94. cout << endl;
  95. if (ipJ == 0 && ipK == 0)
  96. {
  97.  
  98. return ipQ; //ipQ is unchanged
  99. }
  100. if (ipJ == 0 && ipK == 1)
  101. {
  102. ipQ = 0; //ipQ is reset
  103. cout << "In J low K high" << ipQ << endl;
  104.  
  105. }
  106. if (ipJ == 1 && ipK == 0)
  107. {
  108. ipQ = 1;
  109.  
  110. }
  111. if (ipJ == 1 && ipK ==1)
  112. {
  113. if(ipQ == 0)
  114. {
  115. ipQ = 1;
  116. }
  117. else
  118. {
  119. ipQ = 0;
  120. }
  121. }
  122. return ipQ;
  123.  
  124. }
  125.  
  126. JKFlipFlop::~JKFlipFlop()
  127. {
  128.  
  129. }
  130.  
  131.  
  132. };
  133. #endif
  134.  
  135.  
  136. #include "JKFlipFlop.h"
  137. #include <iostream>
  138. using namespace std;
  139. int main()
  140. {
  141. JKFlipFlop FJK;
  142. int ipWireA = 0;
  143. int ipWireB = 0;
  144. int Jtest = 0;
  145. int Ktest = 0;
  146. int Qtest = 0;
  147. int outputWire = 0;
  148.  
  149.  
  150. ipWireA = 0;
  151. Jtest = 0;
  152. Ktest = 0;
  153. Qtest = 0;
  154. outputWire = FJK.enableJK(ipWireA, Jtest, Ktest, Qtest);
  155. cout << "Result of JK (ipWire 0, J low, K low): " << outputWire << endl;
  156.  
  157. cout << endl;
  158. ipWireA = 1;
  159. Jtest = 0;
  160. Ktest = 0;
  161. outputWire = FJK.enableJK(ipWireA, Jtest, Ktest, Qtest);
  162. cout << "Result of JK (ipWire 1, J low, K low): " << outputWire << endl;
  163.  
  164. cout << endl;
  165. ipWireA = 1;
  166. Jtest = 0;
  167. Ktest = 1;
  168. outputWire = FJK.enableJK(ipWireA, Jtest, Ktest, Qtest);
  169. cout << "Result of JK (ipWire 1, J low, K high): " << outputWire << endl;
  170.  
  171. cout << endl;
  172. ipWireA = 1;
  173. Jtest = 1;
  174. Ktest = 0;
  175. outputWire = FJK.enableJK(ipWireA, Jtest, Ktest, Qtest);
  176. cout << "Result of JK (ipWire 1, J high, K low): " << outputWire << endl;
  177.  
  178. cout << endl;
  179. ipWireA = 1;
  180. Jtest = 1;
  181. Ktest = 1;
  182. outputWire = FJK.enableJK(ipWireA, Jtest, Ktest, Qtest);
  183. cout << "Result of JK (ipWire 1, J = 1, K = 1): " << outputWire << endl;
  184.  
  185. return 0;
  186. }
  187.  
  188.  
It's been a while since I've written anything in C++, so it's probably obvious to the experts out there, maybe even to non-experts, but I can't see it and I'm tired of loosing sleep over it, lol. Perhaps it's just my logic, IDK...

Any help would be appreciated.

TMS
Sep 5 '07 #1
5 9179
weaknessforcats
9,208 Expert Mod 8TB
Your default constructor has no code. Therefore, when you create an object, the member variables are garbage:

Expand|Select|Wrap|Line Numbers
  1. 114. JKFlipFlop FJK;          //garbage member variables
  2.  
There's nothing wrong with how you are passing your function arguments.
Sep 5 '07 #2
TMS
119 100+
ok, but when I initialize (for example)

Expand|Select|Wrap|Line Numbers
  1.  
  2. JKFlipFlop::JKFlipFlop()
  3. {
  4. ipJ = 0; 
  5. }
  6.  
it does the same thing (garbage out). So, I'm missing something between that and the copy constructor I use just after this, right? What am I missing ?
Sep 5 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
JKFlipFlop::JKFlipFlop()
{
ipJ = 0;
}
OK fine. But you are not initializing all of the class members. You have to do them all.

Then:
So, I'm missing something between that and the copy constructor I use just after this, right?
You have no copy constructor. That means if you use a JKFlipFlop as a function argument, the compiler will trot out its default copy constructor which will memberwise copy your object. Considering the data members are all int, this should be OK.

However, you aren't calling a function using a JKFlipFlop as an argument so I don't understand what you mean by your using a copy constructor.
Sep 5 '07 #4
TMS
119 100+
granted... that was an example. I wanted to see if that was correct. So, I tried initializing all the members, like what I showed previously, and it still doesn't work.

Here is the sad part... I thought that the constructor I used to set the values was the copy constructor. I think I have it all backwards but I keep getting in an endless loop of wrong... :(
Sep 5 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
I did this:

Expand|Select|Wrap|Line Numbers
  1. JKFlipFlop::JKFlipFlop()
  2. {
  3.     this->ipJ = 0;
  4.     this->ipK = 0;
  5.     this->ipQ = 0;
  6.     this->ipWireJ = 0;
  7.     this->ipWireK = 0;
  8. }
  9.  
and your values look OK. But I didn't follow your logic. I just verified they were initialized.


A constructor is called by the compiler after an object has been created and after the default constructor on every class memeber has been called. So, byt the time you get to the { of the constructor, your object is conmpletely built and ready to go. The purpose of the constructor is to add initializations not already done. In your case, setting the class data members to 0.

A copy constructor is a constructor that takes a const class reference as an argument. It is used when you need to create a new object from an existing one. After the new object is created and after default constructor on every class memeber has been called, you can then add any initializations not already done. See below.

[code=cpp]
class JKFlipFlop
{
public:
JKFlipFlop();
JKFlipFlop(int, int, int, int, int);
JKFlipFlop(const JKFlipFlop& arg); //copy ctor <<<---
void setJandK(int, int, int, int, int);
int JKFlip(int ipJ, int ipK, int ipQ);
int enableJK(int inputWire, int ipJ, int ipK, int ipQ);
~JKFlipFlop();
private:
int ipJ;
int ipK;
int ipQ;
int ipWireJ;
int ipWireK;
};
Sep 5 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: RicercatoreSbadato | last post by:
I have to flip vertically a Bitmap. Is it possible with GDI+ and C#?
0
by: Mr. A | last post by:
I'm looking for a code to flip a window, like in the Tiger - Apple OS. I though of the following algorithm but I can get it implemented right: 1. Capture the window who is about to be flipped....
0
by: coolindienc | last post by:
Honestly, I am totally lost about this program. I don't even know where to start. At first I thought I can ask a user to flip a coin. But then again one will get bored flipping coin fot 100 times....
8
by: coolindienc | last post by:
At first I had no idea where to start. Finally I tried to get somewhere and I am still stuck. I wrote two different ways using while loop and for loop. Below are codes for both. I am getting...
71
by: Murray R. Van Luyn | last post by:
Hi, Since I have made changes to my website it's been a complete flop. According to the logs, as soon as visitors have downloaded the index page they are off. I can't figure out why? ...
7
by: kumana1 | last post by:
Here's my dilema: My database is built around people and their contact info. On my "data entry form" there is a place for their main address, and on the subform which is attached to the main...
6
by: Francesco Moi | last post by:
Hi. I'd like to flip horizontally an image with JavaScript. Is it possible? Thank you very much.
2
by: as | last post by:
Hi folks, am a newbie in C++ world. Normally I use matlab more often. But right now I want to create a Rising Edge D Flip-Flop model which has two Digital inputs, i.e. INPUTDATA and a CLOCK. The...
6
by: dissectcode | last post by:
Is there a quick method to flip endian-ness in C? Different platforms I am using, use big and little endian, and I want to flip everything to big.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.