SlideShare uma empresa Scribd logo
1 de 11
CS-UY 2214 — Homework 2
Jeff Epstein
Introduction
Complete the following exercises. When you finish, submit your
solutions on Gradescope.
Submit your code files exactly as named in the questions.
Problems
1. If you haven’t yet done so, complete the Verilog tutorial,
which you can find on Classes, in the Resources
section, in the “guides” folder. You don’t have to submit
anything: this is just to familiarize you with
the tools.
You may want to look at the Verilog cheat sheet and reference
manual, also available on Classes.
2. Download and extract the enclosed files from hw2a.zip:
• full_adder_nodelay.v – a full one-bit adder module
• four_bit_adder_subtractor.v – a (broken) four-bit ripple-carry
adder/subtractor, which uses
the adder in the above file
• adder_subtractor_test.v – a test bench that will verify the
behavior of the above module
To run the test bench, compile adder_subtractor_test.v and run
the resulting binary in the simu-
lator. View the resulting waveform in GTKWave. The followi ng
commands will do it:
iverilog -o adder_subtractor_test.vvp adder_subtractor_test.v
vvp adder_subtractor_test.vvp
gtkwave adder_subtractor_test.vcd
The four_bit_adder_subtractor module should be able to add and
subtract four bit numbers. As
provided, however, it can currently only add.
The four_bit_adder_subtractor module takes three inputs:
• A – the first input (four bit)
• B – the second input (four bit)
• Op – the operation (one bit); this will be 0 for addition and 1
for subtraction. Initially, it is
ignored within the module.
And it yields two outputs:
• S – the sum (four bit)
• Cout – the carry out (one bit)
1
When you run the Verilog simulator, the test bench will notify
you if the adder produces unexpected
results. Initially, you should see the following output:
When subtracting 1 and 1, got 2, but expected 0
When subtracting 2 and 5, got 7, but expected -3
When subtracting 4 and -4, got 0, but expected -8
When subtracting 7 and 7, got -2, but expected 0
(The numbers might seem a little funny: the test bench claims
that 4 minus −4 should yield −8. This
is because in a 4-bit 2’s complement number, it’s impossible to
represent 8, the mathemtically correct
solution. The bit pattern 1000 will be interpreted as −8.)
By modifying the four_bit_adder_subtractor.v file, make it
support both addition and subtraction.
Do not modify the other files. Your solution must be consistent
with the addition algorithm we
discussed in class. You may not use Verilog’s built-in
arithmetic operators. You may use the conditional
operator (?:) as a multiplexer. You can use all the basic gates
(&, |, ^, ∼). If you’ve done it correctly,
the test bench shouldn’t print out any unexpected results.
Submit your corrected four_bit_adder_subtractor.v file, as well
as a screenshot (named
four_bit_adder_subtractor.png or four_bit_adder_subtractor.jpg)
showing the output of
GTKWave when viewing the waveform of ports on the corrected
four_bit_adder_subtractor module.
Make sure that your waveform doesn’t contain any red
(unknown) values.
3. Write a combinatorial Verilog module binary_to_gray. Its
input, B, is an unsigned 4-bit binary
number. Its 4-bit output, G, is the corresponding gray code. Use
only Verilog’s low-level logical
operations (operators |, &, ^, ∼) in your answer.
Your module with strt like this:
module binary_to_gray(B, G);
Be sure to declare your ports as input or output; declare any
additional wires you may need; and use
assign statements to connect ports to gates.
One way to convert from binary to gray code is to observe the
following rules:
• The MSB (most significant bit) of the output is equal to the
MSB of the input.
• Bit i of the output is obtained by XOR-ing together bits i and i
+ 1 of the input (i.e. that bit,
and the one to its left).
Put your code in a file named binary_to_gray.v. Test your code
using the binary_to_gray_tb.v
test bench file provided in hw2b.zip. Note that the test bench
does not automatically verify the test
cases; you’ll need to look at the waveform in GTKWave in
order to determine if your output is correct.
The correct gray codes are given in binary on the Wikipedia
page linked above.
Submit your source file (named binary_to_gray.v) and a
screenshot of GTKWave showing the wave-
forms of your module’s ports (named binary_to_gray.png or
binary_to_gray.jpg). Make sure that
your waveform doesn’t contain any red (unknown) values.
4. Download the file hw2c.zip.
Using the ALU circuit diagram that we discussed in class as a
guide, implement a combinatorial 4-
bit Arithmetic Logic Unit in Verilog. Your module must be
functionally identical to the unit design
presented in class: it must support a 3-bit operation input that
selects one of the following operations:
A & B, A | B, A + B, A & ∼B, A | ∼B, A - B, A < B. The inputs
and output of the unit should be
4-bit values.
2
https://en.wikipedia.org/wiki/Gray_code
A circuit diagram of the ALU is available in the zip file, as well
as in the class slides.
In your implementation, do not use the high-level Verilog
operators +, -, <, etc. Instead, implement
the ALU’s logic as given in the diagram, using low-level gate
logic (operators |, &, ^, ∼) and an adder.
Use the included four_bit_adder module to provide the adder
functionality. You should not need
more than one instance of this adder. You may use the
conditional operator (?:) as a multiplexer.
You can nest several instances of the conditional operator to
simulate a larger multiplexer.
Use the included alu.v to get started. Test your code using the
included Verilog testbench alu_tb.v.
Submit your completed alu.v.
Hint: to create an instance of four_bit_adder inside your
module, you can use syntax similar to the
following. Below, we’re creating an instance of four_bit_adder
named the_adder; the name does not
matter. We create a 4-bit wire named adder_sum and a 1-bit
wire named adder_Cout for the sum and
carry out of the adder, respectively, which we connect to the
adder. You are responsible for providing
reasonable values for A, B, and carry in.
wire [3:0] adder_sum;
wire adder_Cout;
four_bit_adder the_adder(A, B, 0, adder_sum , adder_Cout );
Hint: you can use the ?: syntax to select from one of several
options. For example:
// X will be 3 when Y is 0, and 4 otherwise.
assign X = (Y==0) ? 3 : 4;
// Z will be 3 when Y is 0, and 4 when Y is 2, and 5 otherwise.
assign Z = (Y==0) ? 3 : (Y==2) ? 4 : 5;
You already know the OR, AND, and XOR operations as
applied to logical (i.e. true/false) inputs. For
example, True and False == False.
These operations can also apply to pairs of n-bit binary numbers
a and b. In this case, the numbers will
be treated as a sequence of bits a(n−1)...0 and b(n−1)...0. The
result of a op b will be the sequence of bits
arising from applying operation op to pairs of bits from each
operand, i.e. an−1 op bn−1, an−2 op bn−2, ...,
a0 op b0. When these operations are applied to sequences of
bits, they are called bitwise operations.
In C and C++, the & operator is bitwise AND, | is bitwise OR, ^
is bitwise XOR, and ∼ is bitwise
NOT. (Distinguish these bitwise operators from their
logical/boolean counterparts: && for logical AND; ||
for logical OR; and ! for logical NOT.)
For example, let’s say we want to calculate 2510 | 510, i.e. the
bitwise OR of 25 and 5. That is, in decimal:
2 5
| 5
?
First, let’s convert both numbers to binary. Here, we write 8
bits for each number:
0 0 0 1 1 0 0 1 = 2510
| 0 0 0 0 0 1 0 1 = 510
?
To calculate the answer, we apply the OR operation to the two
bits in each column, yielding:
3
0 0 0 1 1 0 0 1 = 2510
| 0 0 0 0 0 1 0 1 = 510
0 0 0 1 1 1 0 1
That value, 000111012, can be converted into decimal:
0 0 0 1 1 0 0 1 = 2510
| 0 0 0 0 0 1 0 1 = 510
0 0 0 1 1 1 0 1 = 2910
Therefore 2510 | 510 = 2910.
The unary NOT operation can also be applied bitwise, in which
case each of the bits of the input will be
inverted. For example, to calculate ∼ 2510, we convert the
value into binary, yielding 000110012. Note that
we’ve written the number with 8 bits. Now we invert each bit,
giving us 111001102, which is 23010.
For the following questions, use the C/C++ bit-twiddling
operators &, |, ^, and ∼. You may also find
the operators << and >> helpful.
You don’t need a compiler to verify your work. You should be
able to complete the functions based on
your own calculations.
The following diagram, showing the terminology used to name
individual bits in an 8-bit representation
of the decimal number 41, may help:
5. Complete the following C function, which will return a value
equal to its parameter, with the third
least significant bit flipped on. That is, flipOnBit3(90) should
return 94, but flipOnBit3(45) should
return 45, since the third least significant bit is already on in
45.
unsigned int flipOnBit3(unsigned int x) {
return YOUR CODE HERE;
}
Your solution should be one line. Submit your answer in a plain
text file named hw2.txt. Number
your answer with the question number.
6. Complete the following C function, which will return a value
equal to its parameter, with the second
least significant bit toggled, i.e. if the second least significant
bit is on, turn it off, and if it is off, turn
it on. That is, toggleBit2(90) should return 88, but
toggleBit2(45) should return 47.
Hint: for this question, you probably want to use the xor
operator (^).
unsigned int toggleBit2(unsigned int x) {
return YOUR CODE HERE;
}
Your solution should be one line. Submit your answer in a plain
text file named hw2.txt. Number
your answer with the question number.
4
https://en.wikipedia.org/wiki/Bitwise_operations_in_C#Bitwise
_operators
7. Complete the following C function, which will return a value
equal to its parameter, with the fifth least
significant bit flipped off. That is, flipOffBit5(90) should return
74, but flipOffBit5(45) should
return 45, since the fifth least significant bit is already off in
45.
Do not make any assumptions about the size of an unsigned int.
That is, your solution should work
equally well running on an architecture where unsigned ints are
16-bit, 32-bit, or 64-bit.
unsigned int flipOffBit5(unsigned int x) {
return YOUR CODE HERE;
}
Your solution should be one line. Submit your answer in a plain
text file named hw2.txt. Number
your answer with the question number.
8. Your friend Rupert often confuses the C/C++ logical AND
operator (&&) with the bitwise AND
operator (&). In frustration, he decides to abandon the logical
version, and use the bitwise form
exclusively. Because C and C++ use zero to represent false, and
any nonzero value to represent true,
he opines, using the bitwise operator in place of the logical
operator will produce equivalent results.
Is Rupert correct to make this substitution? Justify your answer
with examples.
5
CS-UY 2214 — Homework 2Jeff EpsteinIntroductionCom

Mais conteúdo relacionado

Semelhante a CS-UY 2214 — Homework 2Jeff EpsteinIntroductionCom

Gsp 215 Believe Possibilities / snaptutorial.com
Gsp 215  Believe Possibilities / snaptutorial.comGsp 215  Believe Possibilities / snaptutorial.com
Gsp 215 Believe Possibilities / snaptutorial.comStokesCope20
 
GSP 215 Enhance teaching/tutorialrank.com
 GSP 215 Enhance teaching/tutorialrank.com GSP 215 Enhance teaching/tutorialrank.com
GSP 215 Enhance teaching/tutorialrank.comjonhson300
 
GSP 215 Inspiring Innovation/tutorialrank.com
GSP 215 Inspiring Innovation/tutorialrank.comGSP 215 Inspiring Innovation/tutorialrank.com
GSP 215 Inspiring Innovation/tutorialrank.comjonhson129
 
GSP 215 RANK Become Exceptional--gsp215rank.com
GSP 215 RANK Become Exceptional--gsp215rank.comGSP 215 RANK Become Exceptional--gsp215rank.com
GSP 215 RANK Become Exceptional--gsp215rank.comclaric119
 
GSP 215 RANK Achievement Education--gsp215rank.com
GSP 215 RANK Achievement Education--gsp215rank.comGSP 215 RANK Achievement Education--gsp215rank.com
GSP 215 RANK Achievement Education--gsp215rank.comclaric169
 
GSP 215 RANK Introduction Education--gsp215rank.com
GSP 215 RANK Introduction Education--gsp215rank.comGSP 215 RANK Introduction Education--gsp215rank.com
GSP 215 RANK Introduction Education--gsp215rank.comagathachristie281
 
GSP 215 RANK Education Counseling--gsp215rank.com
 GSP 215 RANK Education Counseling--gsp215rank.com GSP 215 RANK Education Counseling--gsp215rank.com
GSP 215 RANK Education Counseling--gsp215rank.comwilliamwordsworth40
 
GSP 215 RANK Education Planning--gsp215rank.com
GSP 215 RANK Education Planning--gsp215rank.comGSP 215 RANK Education Planning--gsp215rank.com
GSP 215 RANK Education Planning--gsp215rank.comWindyMiller12
 
Gsp 215 Effective Communication / snaptutorial.com
Gsp 215  Effective Communication / snaptutorial.comGsp 215  Effective Communication / snaptutorial.com
Gsp 215 Effective Communication / snaptutorial.comHarrisGeorg21
 
GSP 215 RANK Education Your Life--gsp215rank.com
GSP 215 RANK Education Your Life--gsp215rank.comGSP 215 RANK Education Your Life--gsp215rank.com
GSP 215 RANK Education Your Life--gsp215rank.comthomashard64
 
GSP 215 RANK Lessons in Excellence-- gsp215rank.com
GSP 215 RANK Lessons in Excellence-- gsp215rank.comGSP 215 RANK Lessons in Excellence-- gsp215rank.com
GSP 215 RANK Lessons in Excellence-- gsp215rank.comRoelofMerwe102
 
GSP 215 RANK Inspiring Innovation--gsp215rank.com
GSP 215 RANK Inspiring Innovation--gsp215rank.com GSP 215 RANK Inspiring Innovation--gsp215rank.com
GSP 215 RANK Inspiring Innovation--gsp215rank.com KeatonJennings102
 
Gsp 215 Future Our Mission/newtonhelp.com
Gsp 215 Future Our Mission/newtonhelp.comGsp 215 Future Our Mission/newtonhelp.com
Gsp 215 Future Our Mission/newtonhelp.comamaranthbeg8
 
GSP 215 Doing by learn/newtonhelp.com
GSP 215 Doing by learn/newtonhelp.comGSP 215 Doing by learn/newtonhelp.com
GSP 215 Doing by learn/newtonhelp.combellflower126
 
GSP 215 Become Exceptional/newtonhelp.com
GSP 215 Become Exceptional/newtonhelp.comGSP 215 Become Exceptional/newtonhelp.com
GSP 215 Become Exceptional/newtonhelp.combellflower148
 
GSP 215 Perfect Education/newtonhelp.com
GSP 215 Perfect Education/newtonhelp.comGSP 215 Perfect Education/newtonhelp.com
GSP 215 Perfect Education/newtonhelp.combellflower169
 
GSP 215 RANK Education Counseling -- gsp215rank.com
GSP 215 RANK Education Counseling -- gsp215rank.comGSP 215 RANK Education Counseling -- gsp215rank.com
GSP 215 RANK Education Counseling -- gsp215rank.comkopiko85
 
Oop with c++ notes unit 01 introduction
Oop with c++ notes   unit 01 introductionOop with c++ notes   unit 01 introduction
Oop with c++ notes unit 01 introductionAnanda Kumar HN
 

Semelhante a CS-UY 2214 — Homework 2Jeff EpsteinIntroductionCom (20)

Gsp 215 Believe Possibilities / snaptutorial.com
Gsp 215  Believe Possibilities / snaptutorial.comGsp 215  Believe Possibilities / snaptutorial.com
Gsp 215 Believe Possibilities / snaptutorial.com
 
GSP 215 Enhance teaching/tutorialrank.com
 GSP 215 Enhance teaching/tutorialrank.com GSP 215 Enhance teaching/tutorialrank.com
GSP 215 Enhance teaching/tutorialrank.com
 
GSP 215 Inspiring Innovation/tutorialrank.com
GSP 215 Inspiring Innovation/tutorialrank.comGSP 215 Inspiring Innovation/tutorialrank.com
GSP 215 Inspiring Innovation/tutorialrank.com
 
GSP 215 RANK Become Exceptional--gsp215rank.com
GSP 215 RANK Become Exceptional--gsp215rank.comGSP 215 RANK Become Exceptional--gsp215rank.com
GSP 215 RANK Become Exceptional--gsp215rank.com
 
GSP 215 RANK Achievement Education--gsp215rank.com
GSP 215 RANK Achievement Education--gsp215rank.comGSP 215 RANK Achievement Education--gsp215rank.com
GSP 215 RANK Achievement Education--gsp215rank.com
 
GSP 215 RANK Introduction Education--gsp215rank.com
GSP 215 RANK Introduction Education--gsp215rank.comGSP 215 RANK Introduction Education--gsp215rank.com
GSP 215 RANK Introduction Education--gsp215rank.com
 
GSP 215 RANK Education Counseling--gsp215rank.com
 GSP 215 RANK Education Counseling--gsp215rank.com GSP 215 RANK Education Counseling--gsp215rank.com
GSP 215 RANK Education Counseling--gsp215rank.com
 
GSP 215 RANK Education Planning--gsp215rank.com
GSP 215 RANK Education Planning--gsp215rank.comGSP 215 RANK Education Planning--gsp215rank.com
GSP 215 RANK Education Planning--gsp215rank.com
 
Gsp 215 Effective Communication / snaptutorial.com
Gsp 215  Effective Communication / snaptutorial.comGsp 215  Effective Communication / snaptutorial.com
Gsp 215 Effective Communication / snaptutorial.com
 
P3
P3P3
P3
 
GSP 215 RANK Education Your Life--gsp215rank.com
GSP 215 RANK Education Your Life--gsp215rank.comGSP 215 RANK Education Your Life--gsp215rank.com
GSP 215 RANK Education Your Life--gsp215rank.com
 
GSP 215 RANK Lessons in Excellence-- gsp215rank.com
GSP 215 RANK Lessons in Excellence-- gsp215rank.comGSP 215 RANK Lessons in Excellence-- gsp215rank.com
GSP 215 RANK Lessons in Excellence-- gsp215rank.com
 
GSP 215 RANK Inspiring Innovation--gsp215rank.com
GSP 215 RANK Inspiring Innovation--gsp215rank.com GSP 215 RANK Inspiring Innovation--gsp215rank.com
GSP 215 RANK Inspiring Innovation--gsp215rank.com
 
Gsp 215 Future Our Mission/newtonhelp.com
Gsp 215 Future Our Mission/newtonhelp.comGsp 215 Future Our Mission/newtonhelp.com
Gsp 215 Future Our Mission/newtonhelp.com
 
GSP 215 Doing by learn/newtonhelp.com
GSP 215 Doing by learn/newtonhelp.comGSP 215 Doing by learn/newtonhelp.com
GSP 215 Doing by learn/newtonhelp.com
 
GSP 215 Become Exceptional/newtonhelp.com
GSP 215 Become Exceptional/newtonhelp.comGSP 215 Become Exceptional/newtonhelp.com
GSP 215 Become Exceptional/newtonhelp.com
 
GSP 215 Perfect Education/newtonhelp.com
GSP 215 Perfect Education/newtonhelp.comGSP 215 Perfect Education/newtonhelp.com
GSP 215 Perfect Education/newtonhelp.com
 
Mechanical Engineering Homework Help
Mechanical Engineering Homework HelpMechanical Engineering Homework Help
Mechanical Engineering Homework Help
 
GSP 215 RANK Education Counseling -- gsp215rank.com
GSP 215 RANK Education Counseling -- gsp215rank.comGSP 215 RANK Education Counseling -- gsp215rank.com
GSP 215 RANK Education Counseling -- gsp215rank.com
 
Oop with c++ notes unit 01 introduction
Oop with c++ notes   unit 01 introductionOop with c++ notes   unit 01 introduction
Oop with c++ notes unit 01 introduction
 

Mais de MargenePurnell14

Introduction              Ideally, program andor policy interventio.docx
Introduction              Ideally, program andor policy interventio.docxIntroduction              Ideally, program andor policy interventio.docx
Introduction              Ideally, program andor policy interventio.docxMargenePurnell14
 
INTRO TO PUBLIC ADMINISTRATIONCase Study 11  Who Brought Bern.docx
INTRO TO PUBLIC ADMINISTRATIONCase Study 11  Who Brought Bern.docxINTRO TO PUBLIC ADMINISTRATIONCase Study 11  Who Brought Bern.docx
INTRO TO PUBLIC ADMINISTRATIONCase Study 11  Who Brought Bern.docxMargenePurnell14
 
IntroductionGDD’s ResultsCandidate’s ResultsGDD C.docx
IntroductionGDD’s ResultsCandidate’s ResultsGDD C.docxIntroductionGDD’s ResultsCandidate’s ResultsGDD C.docx
IntroductionGDD’s ResultsCandidate’s ResultsGDD C.docxMargenePurnell14
 
IntroductionDefine the individual client or community populati.docx
IntroductionDefine the individual client or community populati.docxIntroductionDefine the individual client or community populati.docx
IntroductionDefine the individual client or community populati.docxMargenePurnell14
 
Introduction to Public SpeakingWeek 6 AssignmentIn.docx
Introduction to Public SpeakingWeek 6 AssignmentIn.docxIntroduction to Public SpeakingWeek 6 AssignmentIn.docx
Introduction to Public SpeakingWeek 6 AssignmentIn.docxMargenePurnell14
 
Introduction about topic Intelligence phaseWhat is the .docx
Introduction about topic Intelligence phaseWhat is the .docxIntroduction about topic Intelligence phaseWhat is the .docx
Introduction about topic Intelligence phaseWhat is the .docxMargenePurnell14
 
Introduction A short summary is provided on the case subject and.docx
Introduction A short summary is provided on the case subject and.docxIntroduction A short summary is provided on the case subject and.docx
Introduction A short summary is provided on the case subject and.docxMargenePurnell14
 
Introduction Illiteracy is the inability to read and write a.docx
Introduction Illiteracy is the inability to read and write a.docxIntroduction Illiteracy is the inability to read and write a.docx
Introduction Illiteracy is the inability to read and write a.docxMargenePurnell14
 
Intro to Quality Management Week 3Air Bag Recall.docx
Intro to Quality Management Week 3Air Bag Recall.docxIntro to Quality Management Week 3Air Bag Recall.docx
Intro to Quality Management Week 3Air Bag Recall.docxMargenePurnell14
 
Intro to Quality Management Week 3Air Bag RecallAssignment.docx
Intro to Quality Management Week 3Air Bag RecallAssignment.docxIntro to Quality Management Week 3Air Bag RecallAssignment.docx
Intro to Quality Management Week 3Air Bag RecallAssignment.docxMargenePurnell14
 
INTERVIEW WITH AMERICAN INDIAN COMMUNITY PRACTITIONERSResourcesD.docx
INTERVIEW WITH AMERICAN INDIAN COMMUNITY PRACTITIONERSResourcesD.docxINTERVIEW WITH AMERICAN INDIAN COMMUNITY PRACTITIONERSResourcesD.docx
INTERVIEW WITH AMERICAN INDIAN COMMUNITY PRACTITIONERSResourcesD.docxMargenePurnell14
 
Interview Each team member should interview an educator about his.docx
Interview Each team member should interview an educator about his.docxInterview Each team member should interview an educator about his.docx
Interview Each team member should interview an educator about his.docxMargenePurnell14
 
IntroductionRisk management is critical to protect organization.docx
IntroductionRisk management is critical to protect organization.docxIntroductionRisk management is critical to protect organization.docx
IntroductionRisk management is critical to protect organization.docxMargenePurnell14
 
Interview two different individuals regarding their positions in soc.docx
Interview two different individuals regarding their positions in soc.docxInterview two different individuals regarding their positions in soc.docx
Interview two different individuals regarding their positions in soc.docxMargenePurnell14
 
Internet ExerciseVisit the homepage of Microsoft at www.micros.docx
Internet ExerciseVisit the homepage of Microsoft at www.micros.docxInternet ExerciseVisit the homepage of Microsoft at www.micros.docx
Internet ExerciseVisit the homepage of Microsoft at www.micros.docxMargenePurnell14
 
Interpersonal Violence Against Women, The Role of Men by Martin Schw.docx
Interpersonal Violence Against Women, The Role of Men by Martin Schw.docxInterpersonal Violence Against Women, The Role of Men by Martin Schw.docx
Interpersonal Violence Against Women, The Role of Men by Martin Schw.docxMargenePurnell14
 
Internet of Vehicles-ProjectIntroduction - what you plan t.docx
Internet of Vehicles-ProjectIntroduction - what you plan t.docxInternet of Vehicles-ProjectIntroduction - what you plan t.docx
Internet of Vehicles-ProjectIntroduction - what you plan t.docxMargenePurnell14
 
Interview an ELL instructor from a Title I school about how assessme.docx
Interview an ELL instructor from a Title I school about how assessme.docxInterview an ELL instructor from a Title I school about how assessme.docx
Interview an ELL instructor from a Title I school about how assessme.docxMargenePurnell14
 
INTERNATIONAL JOURNAL OF INFORMATION SECURITY SCIENCE Walid.docx
INTERNATIONAL JOURNAL OF INFORMATION SECURITY SCIENCE  Walid.docxINTERNATIONAL JOURNAL OF INFORMATION SECURITY SCIENCE  Walid.docx
INTERNATIONAL JOURNAL OF INFORMATION SECURITY SCIENCE Walid.docxMargenePurnell14
 
International Finance Please respond to the followingBased on.docx
International Finance Please respond to the followingBased on.docxInternational Finance Please respond to the followingBased on.docx
International Finance Please respond to the followingBased on.docxMargenePurnell14
 

Mais de MargenePurnell14 (20)

Introduction              Ideally, program andor policy interventio.docx
Introduction              Ideally, program andor policy interventio.docxIntroduction              Ideally, program andor policy interventio.docx
Introduction              Ideally, program andor policy interventio.docx
 
INTRO TO PUBLIC ADMINISTRATIONCase Study 11  Who Brought Bern.docx
INTRO TO PUBLIC ADMINISTRATIONCase Study 11  Who Brought Bern.docxINTRO TO PUBLIC ADMINISTRATIONCase Study 11  Who Brought Bern.docx
INTRO TO PUBLIC ADMINISTRATIONCase Study 11  Who Brought Bern.docx
 
IntroductionGDD’s ResultsCandidate’s ResultsGDD C.docx
IntroductionGDD’s ResultsCandidate’s ResultsGDD C.docxIntroductionGDD’s ResultsCandidate’s ResultsGDD C.docx
IntroductionGDD’s ResultsCandidate’s ResultsGDD C.docx
 
IntroductionDefine the individual client or community populati.docx
IntroductionDefine the individual client or community populati.docxIntroductionDefine the individual client or community populati.docx
IntroductionDefine the individual client or community populati.docx
 
Introduction to Public SpeakingWeek 6 AssignmentIn.docx
Introduction to Public SpeakingWeek 6 AssignmentIn.docxIntroduction to Public SpeakingWeek 6 AssignmentIn.docx
Introduction to Public SpeakingWeek 6 AssignmentIn.docx
 
Introduction about topic Intelligence phaseWhat is the .docx
Introduction about topic Intelligence phaseWhat is the .docxIntroduction about topic Intelligence phaseWhat is the .docx
Introduction about topic Intelligence phaseWhat is the .docx
 
Introduction A short summary is provided on the case subject and.docx
Introduction A short summary is provided on the case subject and.docxIntroduction A short summary is provided on the case subject and.docx
Introduction A short summary is provided on the case subject and.docx
 
Introduction Illiteracy is the inability to read and write a.docx
Introduction Illiteracy is the inability to read and write a.docxIntroduction Illiteracy is the inability to read and write a.docx
Introduction Illiteracy is the inability to read and write a.docx
 
Intro to Quality Management Week 3Air Bag Recall.docx
Intro to Quality Management Week 3Air Bag Recall.docxIntro to Quality Management Week 3Air Bag Recall.docx
Intro to Quality Management Week 3Air Bag Recall.docx
 
Intro to Quality Management Week 3Air Bag RecallAssignment.docx
Intro to Quality Management Week 3Air Bag RecallAssignment.docxIntro to Quality Management Week 3Air Bag RecallAssignment.docx
Intro to Quality Management Week 3Air Bag RecallAssignment.docx
 
INTERVIEW WITH AMERICAN INDIAN COMMUNITY PRACTITIONERSResourcesD.docx
INTERVIEW WITH AMERICAN INDIAN COMMUNITY PRACTITIONERSResourcesD.docxINTERVIEW WITH AMERICAN INDIAN COMMUNITY PRACTITIONERSResourcesD.docx
INTERVIEW WITH AMERICAN INDIAN COMMUNITY PRACTITIONERSResourcesD.docx
 
Interview Each team member should interview an educator about his.docx
Interview Each team member should interview an educator about his.docxInterview Each team member should interview an educator about his.docx
Interview Each team member should interview an educator about his.docx
 
IntroductionRisk management is critical to protect organization.docx
IntroductionRisk management is critical to protect organization.docxIntroductionRisk management is critical to protect organization.docx
IntroductionRisk management is critical to protect organization.docx
 
Interview two different individuals regarding their positions in soc.docx
Interview two different individuals regarding their positions in soc.docxInterview two different individuals regarding their positions in soc.docx
Interview two different individuals regarding their positions in soc.docx
 
Internet ExerciseVisit the homepage of Microsoft at www.micros.docx
Internet ExerciseVisit the homepage of Microsoft at www.micros.docxInternet ExerciseVisit the homepage of Microsoft at www.micros.docx
Internet ExerciseVisit the homepage of Microsoft at www.micros.docx
 
Interpersonal Violence Against Women, The Role of Men by Martin Schw.docx
Interpersonal Violence Against Women, The Role of Men by Martin Schw.docxInterpersonal Violence Against Women, The Role of Men by Martin Schw.docx
Interpersonal Violence Against Women, The Role of Men by Martin Schw.docx
 
Internet of Vehicles-ProjectIntroduction - what you plan t.docx
Internet of Vehicles-ProjectIntroduction - what you plan t.docxInternet of Vehicles-ProjectIntroduction - what you plan t.docx
Internet of Vehicles-ProjectIntroduction - what you plan t.docx
 
Interview an ELL instructor from a Title I school about how assessme.docx
Interview an ELL instructor from a Title I school about how assessme.docxInterview an ELL instructor from a Title I school about how assessme.docx
Interview an ELL instructor from a Title I school about how assessme.docx
 
INTERNATIONAL JOURNAL OF INFORMATION SECURITY SCIENCE Walid.docx
INTERNATIONAL JOURNAL OF INFORMATION SECURITY SCIENCE  Walid.docxINTERNATIONAL JOURNAL OF INFORMATION SECURITY SCIENCE  Walid.docx
INTERNATIONAL JOURNAL OF INFORMATION SECURITY SCIENCE Walid.docx
 
International Finance Please respond to the followingBased on.docx
International Finance Please respond to the followingBased on.docxInternational Finance Please respond to the followingBased on.docx
International Finance Please respond to the followingBased on.docx
 

Último

How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesShubhangi Sonawane
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIShubhangi Sonawane
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 

Último (20)

How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 

CS-UY 2214 — Homework 2Jeff EpsteinIntroductionCom

  • 1. CS-UY 2214 — Homework 2 Jeff Epstein Introduction Complete the following exercises. When you finish, submit your solutions on Gradescope. Submit your code files exactly as named in the questions. Problems 1. If you haven’t yet done so, complete the Verilog tutorial, which you can find on Classes, in the Resources section, in the “guides” folder. You don’t have to submit anything: this is just to familiarize you with the tools. You may want to look at the Verilog cheat sheet and reference manual, also available on Classes. 2. Download and extract the enclosed files from hw2a.zip: • full_adder_nodelay.v – a full one-bit adder module • four_bit_adder_subtractor.v – a (broken) four-bit ripple-carry adder/subtractor, which uses the adder in the above file • adder_subtractor_test.v – a test bench that will verify the behavior of the above module
  • 2. To run the test bench, compile adder_subtractor_test.v and run the resulting binary in the simu- lator. View the resulting waveform in GTKWave. The followi ng commands will do it: iverilog -o adder_subtractor_test.vvp adder_subtractor_test.v vvp adder_subtractor_test.vvp gtkwave adder_subtractor_test.vcd The four_bit_adder_subtractor module should be able to add and subtract four bit numbers. As provided, however, it can currently only add. The four_bit_adder_subtractor module takes three inputs: • A – the first input (four bit) • B – the second input (four bit) • Op – the operation (one bit); this will be 0 for addition and 1 for subtraction. Initially, it is ignored within the module. And it yields two outputs: • S – the sum (four bit) • Cout – the carry out (one bit) 1 When you run the Verilog simulator, the test bench will notify you if the adder produces unexpected results. Initially, you should see the following output:
  • 3. When subtracting 1 and 1, got 2, but expected 0 When subtracting 2 and 5, got 7, but expected -3 When subtracting 4 and -4, got 0, but expected -8 When subtracting 7 and 7, got -2, but expected 0 (The numbers might seem a little funny: the test bench claims that 4 minus −4 should yield −8. This is because in a 4-bit 2’s complement number, it’s impossible to represent 8, the mathemtically correct solution. The bit pattern 1000 will be interpreted as −8.) By modifying the four_bit_adder_subtractor.v file, make it support both addition and subtraction. Do not modify the other files. Your solution must be consistent with the addition algorithm we discussed in class. You may not use Verilog’s built-in arithmetic operators. You may use the conditional operator (?:) as a multiplexer. You can use all the basic gates (&, |, ^, ∼). If you’ve done it correctly, the test bench shouldn’t print out any unexpected results. Submit your corrected four_bit_adder_subtractor.v file, as well as a screenshot (named four_bit_adder_subtractor.png or four_bit_adder_subtractor.jpg) showing the output of GTKWave when viewing the waveform of ports on the corrected four_bit_adder_subtractor module. Make sure that your waveform doesn’t contain any red (unknown) values. 3. Write a combinatorial Verilog module binary_to_gray. Its input, B, is an unsigned 4-bit binary number. Its 4-bit output, G, is the corresponding gray code. Use
  • 4. only Verilog’s low-level logical operations (operators |, &, ^, ∼) in your answer. Your module with strt like this: module binary_to_gray(B, G); Be sure to declare your ports as input or output; declare any additional wires you may need; and use assign statements to connect ports to gates. One way to convert from binary to gray code is to observe the following rules: • The MSB (most significant bit) of the output is equal to the MSB of the input. • Bit i of the output is obtained by XOR-ing together bits i and i + 1 of the input (i.e. that bit, and the one to its left). Put your code in a file named binary_to_gray.v. Test your code using the binary_to_gray_tb.v test bench file provided in hw2b.zip. Note that the test bench does not automatically verify the test cases; you’ll need to look at the waveform in GTKWave in order to determine if your output is correct. The correct gray codes are given in binary on the Wikipedia page linked above. Submit your source file (named binary_to_gray.v) and a screenshot of GTKWave showing the wave- forms of your module’s ports (named binary_to_gray.png or binary_to_gray.jpg). Make sure that your waveform doesn’t contain any red (unknown) values. 4. Download the file hw2c.zip.
  • 5. Using the ALU circuit diagram that we discussed in class as a guide, implement a combinatorial 4- bit Arithmetic Logic Unit in Verilog. Your module must be functionally identical to the unit design presented in class: it must support a 3-bit operation input that selects one of the following operations: A & B, A | B, A + B, A & ∼B, A | ∼B, A - B, A < B. The inputs and output of the unit should be 4-bit values. 2 https://en.wikipedia.org/wiki/Gray_code A circuit diagram of the ALU is available in the zip file, as well as in the class slides. In your implementation, do not use the high-level Verilog operators +, -, <, etc. Instead, implement the ALU’s logic as given in the diagram, using low-level gate logic (operators |, &, ^, ∼) and an adder. Use the included four_bit_adder module to provide the adder functionality. You should not need more than one instance of this adder. You may use the conditional operator (?:) as a multiplexer. You can nest several instances of the conditional operator to simulate a larger multiplexer. Use the included alu.v to get started. Test your code using the included Verilog testbench alu_tb.v. Submit your completed alu.v. Hint: to create an instance of four_bit_adder inside your module, you can use syntax similar to the
  • 6. following. Below, we’re creating an instance of four_bit_adder named the_adder; the name does not matter. We create a 4-bit wire named adder_sum and a 1-bit wire named adder_Cout for the sum and carry out of the adder, respectively, which we connect to the adder. You are responsible for providing reasonable values for A, B, and carry in. wire [3:0] adder_sum; wire adder_Cout; four_bit_adder the_adder(A, B, 0, adder_sum , adder_Cout ); Hint: you can use the ?: syntax to select from one of several options. For example: // X will be 3 when Y is 0, and 4 otherwise. assign X = (Y==0) ? 3 : 4; // Z will be 3 when Y is 0, and 4 when Y is 2, and 5 otherwise. assign Z = (Y==0) ? 3 : (Y==2) ? 4 : 5; You already know the OR, AND, and XOR operations as applied to logical (i.e. true/false) inputs. For example, True and False == False. These operations can also apply to pairs of n-bit binary numbers a and b. In this case, the numbers will be treated as a sequence of bits a(n−1)...0 and b(n−1)...0. The result of a op b will be the sequence of bits arising from applying operation op to pairs of bits from each operand, i.e. an−1 op bn−1, an−2 op bn−2, ..., a0 op b0. When these operations are applied to sequences of
  • 7. bits, they are called bitwise operations. In C and C++, the & operator is bitwise AND, | is bitwise OR, ^ is bitwise XOR, and ∼ is bitwise NOT. (Distinguish these bitwise operators from their logical/boolean counterparts: && for logical AND; || for logical OR; and ! for logical NOT.) For example, let’s say we want to calculate 2510 | 510, i.e. the bitwise OR of 25 and 5. That is, in decimal: 2 5 | 5 ? First, let’s convert both numbers to binary. Here, we write 8 bits for each number: 0 0 0 1 1 0 0 1 = 2510 | 0 0 0 0 0 1 0 1 = 510 ? To calculate the answer, we apply the OR operation to the two bits in each column, yielding: 3 0 0 0 1 1 0 0 1 = 2510 | 0 0 0 0 0 1 0 1 = 510 0 0 0 1 1 1 0 1
  • 8. That value, 000111012, can be converted into decimal: 0 0 0 1 1 0 0 1 = 2510 | 0 0 0 0 0 1 0 1 = 510 0 0 0 1 1 1 0 1 = 2910 Therefore 2510 | 510 = 2910. The unary NOT operation can also be applied bitwise, in which case each of the bits of the input will be inverted. For example, to calculate ∼ 2510, we convert the value into binary, yielding 000110012. Note that we’ve written the number with 8 bits. Now we invert each bit, giving us 111001102, which is 23010. For the following questions, use the C/C++ bit-twiddling operators &, |, ^, and ∼. You may also find the operators << and >> helpful. You don’t need a compiler to verify your work. You should be able to complete the functions based on your own calculations. The following diagram, showing the terminology used to name individual bits in an 8-bit representation of the decimal number 41, may help: 5. Complete the following C function, which will return a value equal to its parameter, with the third least significant bit flipped on. That is, flipOnBit3(90) should return 94, but flipOnBit3(45) should return 45, since the third least significant bit is already on in 45. unsigned int flipOnBit3(unsigned int x) {
  • 9. return YOUR CODE HERE; } Your solution should be one line. Submit your answer in a plain text file named hw2.txt. Number your answer with the question number. 6. Complete the following C function, which will return a value equal to its parameter, with the second least significant bit toggled, i.e. if the second least significant bit is on, turn it off, and if it is off, turn it on. That is, toggleBit2(90) should return 88, but toggleBit2(45) should return 47. Hint: for this question, you probably want to use the xor operator (^). unsigned int toggleBit2(unsigned int x) { return YOUR CODE HERE; } Your solution should be one line. Submit your answer in a plain text file named hw2.txt. Number your answer with the question number. 4 https://en.wikipedia.org/wiki/Bitwise_operations_in_C#Bitwise _operators 7. Complete the following C function, which will return a value equal to its parameter, with the fifth least significant bit flipped off. That is, flipOffBit5(90) should return
  • 10. 74, but flipOffBit5(45) should return 45, since the fifth least significant bit is already off in 45. Do not make any assumptions about the size of an unsigned int. That is, your solution should work equally well running on an architecture where unsigned ints are 16-bit, 32-bit, or 64-bit. unsigned int flipOffBit5(unsigned int x) { return YOUR CODE HERE; } Your solution should be one line. Submit your answer in a plain text file named hw2.txt. Number your answer with the question number. 8. Your friend Rupert often confuses the C/C++ logical AND operator (&&) with the bitwise AND operator (&). In frustration, he decides to abandon the logical version, and use the bitwise form exclusively. Because C and C++ use zero to represent false, and any nonzero value to represent true, he opines, using the bitwise operator in place of the logical operator will produce equivalent results. Is Rupert correct to make this substitution? Justify your answer with examples. 5