|
Page 1 of 3 If there is a single characteristic that sets C programmers apart, it is an understanding of bits. If you don’t understand bits then you are a programmer in some other language pretending to be a C programmer. Harsh, but true. Find out why the bit has an important role to play in this extract from my book, Deep C Dives.
Deep C Dives Adventures in C
By Mike James

Buy from Amazon.
Contents
Preface Prolog C Dive
- All You Need Are Bits
Extract: All You Need Are Bits ***NEW!
- These aren’t the types you’re looking for
- Type Casting
- Expressions
- Bits and More Bits
Extract: Bits!
- The Brilliant But Evil for
- Into the Void
- Blocks, Stacks and Locals
- Static Storage
- Pointers
- The Array and Pointer Arithmetic
- Heap, The Third Memory Allocation
- First Class Functions
Extract: First Class Functions
- Structs and Objects
Extract: Value Structs
- The Union
- Undefined Behavior
- Exceptions and the Long Jump
<ASIN:B0D6LZZQ8R>
Dive 1
All You Need Are Bits
“Bits are the raw material out of which we fashion the world.”
Most accounts of bit patterns start off from binary values then move on to two’s complement and finally, sometimes floating-point, but this simply hides the fact that bits aren’t any of these things. Let’s start at the beginning.
States
A bit is a two-state system – a bit can be a 1 or a 0. It doesn’t matter what you call these two states, true and false will do, but so will up and down or on and off and many more. Computers work in terms of groups of bits which can be accessed and worked with as a single unit. Typically the size of the group is eight bits, that is a byte. While this isn’t strictly necessary, we’ll use it in our examples which would be similar for a different number of bits.
The eight bits in a byte are usually have a particular order and are generally numbered starting from the right:
7 6 5 4 3 2 1 0
x x x x x x x x
Bit 0 is generally called the “lowest” or “least significant bit” and bit 7 is called the “highest” or “most significant bit”. The reason for this terminology comes from regarding the group of bits as a binary number. Notice that we need to make an “endian” decision. Which end to number bits from is arbitrary and depends on the machine’s architecture, but this big-endian numbering is almost universal. The reason for this will become apparent when we deal with place value systems later.
Any bit in a byte can represent one of two states. For example, you could represent the state of a set of eight lights one per bit position corresponding to on and off. Storing a bit pattern into the byte would set the lights to a particular state and reading the bit pattern would give the state of the lights – notice no numbers were involved in this transaction.
Consider now what happens if a room has two lights in it, a left and a right. Now we have four possible states:
left light off right light off 0 0
left light off right light on 0 1
left light on right light off 1 0
left light on right light on 1 1
So, if you have four states to represent, we need to use two bits, eight states need three bits, 16 states four bits and so on. If you have a number of states that doesn’t fit exactly into n bits then you simply combine some of the extra states. For example:
no lights on 0 0
one light on 0 1 or 1 0
two lights on 1 1
is how you might represent three states using two bits, but there are alternatives.
Again no numbers are involved in this – only bits.
|