Seven-Segment Devices

February 27, 2018

We have today a simple exercise from Jon Bentley’s book Programming Pearls, Chapter 3, Problem 8:

[S. C. Johnson] Seven-segment devices provide an inexpensive display of the ten decimal digits:

 -----           -----   -----           -----   -----   -----   -----   -----
|     |       |       |       | |     | |       |             | |     | |     |
|     |       |       |       | |     | |       |             | |     | |     |
|     |       |       |       | |     | |       |             | |     | |     |
                 -----   -----   -----   -----   -----           -----   -----
|     |       | |             |       |       | |     |       | |     |       |
|     |       | |             |       |       | |     |       | |     |       |
|     |       | |             |       |       | |     |       | |     |       |
 -----           -----   -----           -----   -----           -----   -----

The seven segments are usually numbered as:

 --2--
|     |
3     4
|     |
 --1--
|     |
5     6
|     |
 --0--

Write a program that displays a 16-bit positive integer in five seven-segment digits. The output is an array of five bytes; bit i of byte j is one if and only if the ith segment of digit j should be on.

It was harder to type those digits than it looks.

Your task is to write a program to display numbers using seven-segment digits, as Bentley directs. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Advertisement

Pages: 1 2