notebook/notes/encoding/binary.md

8.9 KiB

title TARGET DECK FILE TAGS tags
Binary Obsidian::STEM binary
binary

Overview

A binary digit or bit is a 0 or 1 character. A bit string is then a contiguous sequence of bits. It's weight is a reference to the number of 1s in the bit string. Compare the below operation to the method for converting from one numerical base to another (e.g. radices#Hexadecimal).

unsigned int bit_weight(int64_t n) {
  unsigned int count = 0;
  while (n) {
    count += (n % 2 == 0) ? 0 : 1;
    n /= 2;
  }
  return count;
}

%%ANKI Basic Why is a "bit" named the way it is? Back: It is short for binary digit. Reference: Oscar Levin, Discrete Mathematics: An Open Introduction, 3rd ed., n.d., https://discrete.openmathbooks.org/pdfs/dmoi3-tablet.pdf.

END%%

%%ANKI Basic What does the weight of a bit string refer to? Back: The number of 1s in the string. Reference: Oscar Levin, Discrete Mathematics: An Open Introduction, 3rd ed., n.d., https://discrete.openmathbooks.org/pdfs/dmoi3-tablet.pdf.

END%%

%%ANKI Basic How might you use C to find the weight of a bit string? Back: Repeatedly divide by 2, counting all remainders of 1. Reference: Oscar Levin, Discrete Mathematics: An Open Introduction, 3rd ed., n.d., https://discrete.openmathbooks.org/pdfs/dmoi3-tablet.pdf. Tags: c17

END%%

%%ANKI Cloze A byte consists of {8} bits. Reference: Bryant, Randal E., and David O'Hallaron. Computer Systems: A Programmer's Perspective. Third edition, Global edition. Always Learning. Pearson, 2016.

END%%

%%ANKI Cloze A byte consists of {2} nibbles. Reference: Bryant, Randal E., and David O'Hallaron. Computer Systems: A Programmer's Perspective. Third edition, Global edition. Always Learning. Pearson, 2016.

END%%

%%ANKI Cloze A nibble consists of {4} bits. Reference: Bryant, Randal E., and David O'Hallaron. Computer Systems: A Programmer's Perspective. Third edition, Global edition. Always Learning. Pearson, 2016.

END%%

%%ANKI Basic What process is used to convert from e.g. decimal to another base? Back: Divide repeatedly by the base. Maintain remainders right to left. Reference: Bryant, Randal E., and David O'Hallaron. Computer Systems: A Programmer's Perspective. Third edition, Global edition. Always Learning. Pearson, 2016.

END%%

%%ANKI Basic Why does converting from e.g. decimal to another base involve repeated division? Back: The position of a digit corresponds to the base raised to that position. Reference: Bryant, Randal E., and David O'Hallaron. Computer Systems: A Programmer's Perspective. Third edition, Global edition. Always Learning. Pearson, 2016.

END%%

Endianness

Platforms with multi-byte objects must establish the object's address and byte ordering. Objects are typically addressed by the smallest address of the bytes used. Bytes are ordered either in big-endian or little-endian. In big-endian, the most significant byte is listed first. In little-endian, the least significant byte is ordered first.

%%ANKI Basic Platforms with multi-byte objects must establish what two conventions? Back: The object's address and byte ordering. Reference: Bryant, Randal E., and David O'Hallaron. Computer Systems: A Programmer's Perspective. Third edition, Global edition. Always Learning. Pearson, 2016.

END%%

%%ANKI Basic How are multi-byte objects typically addressed? Back: By the smallest address of the bytes used. Reference: Bryant, Randal E., and David O'Hallaron. Computer Systems: A Programmer's Perspective. Third edition, Global edition. Always Learning. Pearson, 2016.

END%%

%%ANKI Basic How are bytes of multi-byte objects typically ordered? Back: As big-endian or little-endian. Reference: Bryant, Randal E., and David O'Hallaron. Computer Systems: A Programmer's Perspective. Third edition, Global edition. Always Learning. Pearson, 2016.

END%%

%%ANKI Basic What is endianness? Back: The ordering of bytes of a multi-byte object. Reference: Bryant, Randal E., and David O'Hallaron. Computer Systems: A Programmer's Perspective. Third edition, Global edition. Always Learning. Pearson, 2016.

END%%

%%ANKI Basic What does it mean for a byte to be "most significant"? Back: It contribute most to the byte's (decimal) value. Reference: Bryant, Randal E., and David O'Hallaron. Computer Systems: A Programmer's Perspective. Third edition, Global edition. Always Learning. Pearson, 2016.

END%%

%%ANKI Basic What does it mean for a byte to be "least significant"? Back: It contribute least to the byte's (decimal) value. Reference: Bryant, Randal E., and David O'Hallaron. Computer Systems: A Programmer's Perspective. Third edition, Global edition. Always Learning. Pearson, 2016.

END%%

%%ANKI Basic What does it mean to be big-endian? Back: The most significant byte is ordered first. Reference: Bryant, Randal E., and David O'Hallaron. Computer Systems: A Programmer's Perspective. Third edition, Global edition. Always Learning. Pearson, 2016.

END%%

%%ANKI Basic What does it mean to be little-endian? Back: The least significant byte is ordered first. Reference: Bryant, Randal E., and David O'Hallaron. Computer Systems: A Programmer's Perspective. Third edition, Global edition. Always Learning. Pearson, 2016.

END%%

%%ANKI Basic How are bytes of int32_t x = 0x01234567 written in big-endian? Back: 0x01 0x23 0x45 0x67 Reference: Bryant, Randal E., and David O'Hallaron. Computer Systems: A Programmer's Perspective. Third edition, Global edition. Always Learning. Pearson, 2016. Tags: c17

END%%

%%ANKI Basic How are bytes of int32_t x = 0x01234567 written in little-endian? Back: 0x67 0x45 0x23 0x01 Reference: Bryant, Randal E., and David O'Hallaron. Computer Systems: A Programmer's Perspective. Third edition, Global edition. Always Learning. Pearson, 2016. Tags: c17

END%%

%%ANKI Cloze Many microprocessors chips are {bi-endian} meaning they can be {configured as either big- or little-endian}. Reference: Bryant, Randal E., and David O'Hallaron. Computer Systems: A Programmer's Perspective. Third edition, Global edition. Always Learning. Pearson, 2016.

END%%

%%ANKI Cloze A big-endian system stores the {most} significant byte at the {smallest} memory address. Reference: “Endianness,” in Wikipedia, December 19, 2024, https://en.wikipedia.org/w/index.php?title=Endianness.

END%%

%%ANKI Cloze A big-endian system stores the {least} significant byte at the {largest} memory address. Reference: “Endianness,” in Wikipedia, December 19, 2024, https://en.wikipedia.org/w/index.php?title=Endianness.

END%%

%%ANKI Cloze A little-endian system stores the {least} significant byte at the {smallest} memory address. Reference: “Endianness,” in Wikipedia, December 19, 2024, https://en.wikipedia.org/w/index.php?title=Endianness.

END%%

%%ANKI Cloze A little-endian system stores the {most} significant byte at the {largest} memory address. Reference: “Endianness,” in Wikipedia, December 19, 2024, https://en.wikipedia.org/w/index.php?title=Endianness.

END%%

%%ANKI Basic Which of little- or big-endian more closely mirrors the way numbers are written in English? Back: Big-endian. Reference: “Endianness,” in Wikipedia, December 19, 2024, https://en.wikipedia.org/w/index.php?title=Endianness.

END%%

#include <stdint.h>
#include <stdio.h>

int main() {
  int32_t x = 0x01234567;
  for (int i = 0; i < 4; ++i) {
    printf("%.2x ", ((unsigned char *)(&x))[i]);
  }
}

The above snippet can be used to check endianness on the current machine. If big-endian, the output should be 01 23 45 67. If little-endian, 67 45 23 01.

Bibliography