I’ve written about binary fractions before, though to study how they’re stored in a real number requires more depth (see last week’s Lesson). Essentially, binary fractions are just negative powers of two.
Continue reading
Counting Without Looping – Solution
The challenge for this month’s Exercise is to code a program that loops n times without using a looping keyword or statement. As you can probably guess, the solution involves recursion.
Continue reading
The Magic Behind Real Numbers
Continuing from last week’s Lesson, the puzzle I’m trying to solve is how the computer manipulates the integer values zero, 0x82 and 0x460000 to arrive at 13.375. The answer lies in the binary32 format used to represent real numbers. To store 13.375 in a binary format involves a bit of conjuring.
Continue reading
Counting Without Looping
Difficulty: ★ ★ ★ ★
One of the first lessons taught for any budding programmer is to to output sequential values, say one through ten. I use this example in my books to demonstrate how loops work. You can use for, while, and even goto to make the magic happen in the C language. But can you code such a program without using any looping keywords?
Continue reading
Splitting Up a Real Number
In the C language, the flexible storage provided by a union allows me to access and examine the bits stored in a real number. Due to different data types accessing the same data, I can look at the bits in a float as a long int. From there, I can output the individual bits to see what each one represents, which was covered in last week’s Lesson. In this Lesson, I update the code to examine the individual parts of the binary32 format.
Continue reading
How Real Numbers are Stored in Binary
A computer’s native tongue is binary, the famous ones and zeros worshiped by early mainframe programmers. Clumped into bytes and words, these digits easily represent an armada of integers, both positive and negative values. But what about fractions, decimal values, huge numbers, and teeny tiny numbers? What’s the secret to their storage?
Continue reading
Peeking at Data Stored in a Union
A C language union is a shared data space accessed via different data types. This feature can be handy, providing that you tag the union data as was seen in this Lesson. But I’m after exploiting a union for a more abusive yet useful purpose.
Continue reading
Tagging a Union
A union by itself presents some issues, which is why unions are flagged as insecure. I’ve shown some examples in last week’s Lesson. But there is a solution that provides a backstop for consistency when accessing the different data types stored as union members.
Continue reading
The Clock Angle Problem – Solution
The challenge for this month’s Exercise is to report the angle between two hands on an analog clock. The result must be positive and it must reflect the lesser of two possible degrees.
Continue reading
The Scary Part of the Union
From last week’s Lesson, you saw an example of how different data types stored in a union can be beneficial. As both structures WORDREGS and BYTEREGS overlapped to reflect the data stored in an 8086 CPU, using a union to represent the data is both practical and economic. But such conditions are rare.
Continue reading