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...
Describe some properties of the Gray code? Write the logic function for an 8-bit binary code to 8-bit Gray code function (i.e. g3 = b3)? Write the logic function for an 8-bit Gray code to 8-bit binary code function (i.e. b3 = g3)? Show the 4-bit Gray code output given the 4-bit binary code input. Show the 4-bit binary code output given the 4-bit Gray code input.
Implement the binary reflected Gray code algorithm for generating subsets in Java
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.)
Implement the cmos design for 4 bit grey code to 4 bit binary converter using Verilog (NOTE that you should produce a switch level Verilog implementation using pmos and nmos transistors)
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.)
Q4) [5 points] Consider the following two algorithms: ALGORITHM 1 Bin Rec(n) //Input: A positive decimal integer n llOutput: The number of binary digits in "'s binary representation if n1 return 1 else return BinRec(ln/2)) +1 ALGORITHM 2 Binary(n) tive decimal integer nt io 's binary representation //Output: The number of binary digits in i's binary representation count ←1 while n >1 do count ← count + 1 return count a. Analyze the two algorithms and find the efficiency for...