a. Generate the binary reflexive Gray code of order 4.
b. Trace the following nonrecursive algorithm to generate the
binary reflexive
Gray code of order 4. Start with the n-bit string of all 0
Answer:
a) Generating the Binary Reflexive Gray code of order 4:
#include <bitset>
#include <iostream>
#include <string>
uint32_t gray_encode(uint32_t b)
{
return b ^ (b >> 1);
}
uint32_t gray_decode(uint32_t g)
{
for (uint32_t bit = 1U << 4; bit > 1; bit >>=
1)
{
if (g & bit) g ^= bit >> 1;
}
return g;
}
std::string to_binary(int value) // utility function
{
const std::bitset<4> bs(value);
const std::string str(bs.to_string());
const size_t pos(str.find('1'));
return pos == std::string::npos ? "0" : str.substr(pos);
}
int main()
{
std::cout << "Number\tBinary\tGray\tDecoded\n";
for (uint32_t n = 0; n < 4; ++n)
{
uint32_t g = gray_encode(n);
uint32_t b = gray_decode(g);
std::cout << n << "\t" << to_binary(n) <<
"\t" << to_binary(g) << "\t" << b <<
"\n";
}
}
b) For Gray code 1 bit should be 0 1,
for 2 bit should be 00 01 11 10 etc.
This algorithm does not start with lower values of n. All
strings it generates have the same length,
and the i-th (for i = 1, ..., 2n-1) string is generated from the
(i-1)-th one.
Here is the fist few steps for n = 4:
Start with G0 = 0000
To generate G1, flip 0-th bit in G0, as 0 is the position of the least significant 1 in the binary representation of 1 = 0001b. G1 = 0001.
To generate G2, flip 1-st bit in G1, as 1 is the position of the least significant 1 in the binary representation of 2 = 0010b. G2 = 0011.
To generate G3, flip 0-th bit in G2, as 0 is the position of the least significant 1 in the binary representation of 3 = 0011b. G3 = 0010.
To generate G4, flip 2-nd bit in G3, as 2 is the position of the least significant 1 in the binary representation of 4 = 0100b. G4 = 0110.
To generate G5, flip 0-th bit in G4, as 0 is the position of the least significant 1 in the binary representation of 5 = 0101b. G5 = 0111.
a. Generate the binary reflexive Gray code of order 4. b. Trace the following nonrecursive algorithm...
Exercise 5.6: Generic Binary-to-Gray Converter The regular binary code, which consists of code words ordered according to their increas ing unsigned decimal values, constitutes the most commonly used digital code. In some kind operation opcode 000 ya+b a(N-1:0) b(N-1:0) a(N-1:0) unsigned ya b 001 y(N- 1:0) Arithmetic Arithmetic 010 y-a+b ya+b+cin y(N:0) b(N-1:0 circuit circuit 011 cout cin cin 100 ya+b signed 101 ya b opcode(2:0) opcode(2:0) 110 y-a+b (a) (b) (c) 111 ya+b+cin Figure 5.14 applications, however, gray code...
1. Implement an algorithm to convert binary number into an integer. Binary number is represented as a string. Ex. int n = to_int("01010"); int to_int(const std::string& b) { } 2. Implement an algorithm to convert a decimal number to binary. The return type is string which holds the binary number as string. std::string to_binary(int n) { } 3. Implement a function to check if the number is positive or negative. The function should return true if number is positive. bool...
Design a 4-bit grey code adder. b) The adder has three components: two 4-bit grey-to-binary converters, a 4-bit binary adder, and a 5-bit binary-to-grey code convertor. c) Model this design with SV as a combinational block. d) Write one test bench to verify the SV model. it will receive a grey input that then will be converter into binary to be added then out putting from binary back to gray
Design and implement a 4 bit- binary to gray code converter using CMOS transistors. (30 Marks) (Note: Students are expected to design the circuit with truth table, solve the output expression (by use of K Map or suitable circuit Reduction technique) and implement using CMOS transistors.)
Discrete Mathematics Trace the execution of the binary search algorithm given the following list of values: 3, 5, 6, 8, 9, 11, 14, 16 and the target value 5. Give the state of the list after each time it is divided (the start and end positions of the list). How many times does the while loop execute?
Problem 1: Implement an algorithm to generate prime numbers. You will need to implement the following ingredients (some of them you developed for earlier assignments): 1. A method to generate random binary numbers with n-digits (hint: for the most significant digit, you have no choice, it will be 1; similarly, for the least significant digit there is no choice, it will have to be 1; for all other position, generate 0 or 1 at random) 2. A method to compute...
Design and implement a 4 bit- gray to binary code converter using CMOS transistors. (Note: Students are expected to design the circuit with truth table, solve the output expression (by use of K Map or suitable circuit Reduction technique) and implement using CMOS transistors.)
3. (12 pts) Determine whether the following binary relation is: (1) reflexive, (2) symmetric, (3) antisymmetric, (4) transitive. a) The relation Ron Z where aRb means a = b. Circle your answers. (4 pts) Ris Reflexive? Symmetric? Antisymmetric? Transitive? Yes or No Yes or No Yes or No Yes or No b) The relation R on the set of all people where aRb means that a is taller than b. Circle your answers. (4 pts) Ris Reflexive? Symmetric? Antisymmetric? Transitive?...
CAN SOMEONE HELP!
The following problem is to design an algorithm which check if a binary tree is a binary search tree. The following code was given. There exists a bug in this code for the variable last printed. (20 points) 6. Find the bug and provide a way to fix this bug: public static Integer last printed-null: public static boolean checkBST (TreeNode n) if (nnull) return true // check/ recurse left if (checkBST (n.left)) return false; /I check current...
Finite state machine (FSM) counter design: Gray
codes have a useful property in that consecutive numbers differ in
only a single bit position. Table 1 lists a 3-bit modulo 8 Gray
code representing the numbers 0 to 7. Design a 3-bit modulo 8 Gray
code counter FSM.
a) First design and sketch a 3-bit modulo 8 Gray code counter
FSM with no inputs and three outputs, the 3-bit signal
Q2:0. (A modulo N counter counts from 0 to N −...