Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,844 questions

55,671 answers

573 users

How to print the numbers 0 to 31 by changing 1 bit each time in Ruby

2 Answers

0 votes
# ===========================================================
# Title: Gray‑Code Sequence (One‑Bit‑Change Order)
#
# This program prints the 5‑bit Gray‑code sequence from 0 to 31.
# Gray code guarantees that each successive value differs by
# exactly one bit.
#
# Gray code formula:
#     gray(n) = n ^ (n >> 1)
#
# The program prints the Gray‑code values themselves in the
# natural one‑bit‑change order: 0, 1, 3, 2, 6, 7, 5, 4, ...
# ===========================================================

# Convert an integer to a 5‑bit binary string
def to_bits(value)
  value.to_s(2).rjust(5, "0")
end

# Print the Gray‑code sequence in one‑bit‑change order
def print_gray_sequence
  (0..31).each do |n|
    g = n ^ (n >> 1)   # Gray‑code transformation
    puts "#{g.to_s.rjust(2)}  ->  #{to_bits(g)}"
  end
end

print_gray_sequence



=begin
run:

 0  ->  00000
 1  ->  00001
 3  ->  00011
 2  ->  00010
 6  ->  00110
 7  ->  00111
 5  ->  00101
 4  ->  00100
12  ->  01100
13  ->  01101
15  ->  01111
14  ->  01110
10  ->  01010
11  ->  01011
 9  ->  01001
 8  ->  01000
24  ->  11000
25  ->  11001
27  ->  11011
26  ->  11010
30  ->  11110
31  ->  11111
29  ->  11101
28  ->  11100
20  ->  10100
21  ->  10101
23  ->  10111
22  ->  10110
18  ->  10010
19  ->  10011
17  ->  10001
16  ->  10000

=end

 



answered 2 days ago by avibootz
0 votes
# ===========================================================
# Title: Gray‑Code Table (Numbers 1..31 with Bit Patterns)
#
# This program generates 5‑bit Gray‑code values for numbers 0–31.
# Gray code ensures that each successive value differs by exactly
# one bit.
#
# The program prints numbers 1..31 in normal numeric order,
# while showing their corresponding Gray‑code bit patterns.
# ===========================================================

# Convert an integer to a 5‑bit binary string
def to_bits(value)
  value.to_s(2).rjust(5, "0")
end

# Print the Gray‑code table in numeric order
def print_gray_table(gray)
  (1...gray.length).each do |n|
    puts "#{n.to_s.rjust(2)}  ->  #{to_bits(gray[n])}"
  end
end

# Generate Gray‑code values for 0..31
gray = Array.new(32)
(0..31).each do |n|
  gray[n] = n ^ (n >> 1)
end

# Print numbers 1..31 with their Gray‑code bit patterns
print_gray_table(gray)



=begin
run:

 1  ->  00001
 2  ->  00011
 3  ->  00010
 4  ->  00110
 5  ->  00111
 6  ->  00101
 7  ->  00100
 8  ->  01100
 9  ->  01101
10  ->  01111
11  ->  01110
12  ->  01010
13  ->  01011
14  ->  01001
15  ->  01000
16  ->  11000
17  ->  11001
18  ->  11011
19  ->  11010
20  ->  11110
21  ->  11111
22  ->  11101
23  ->  11100
24  ->  10100
25  ->  10101
26  ->  10111
27  ->  10110
28  ->  10010
29  ->  10011
30  ->  10001
31  ->  10000

=end

 



answered 2 days ago by avibootz

Related questions

...