Bits, Bytes, Bit-level manipulations

Bits:
The smallest unit for computers to store information. A bit is either 0 or 1. The word “bit” is from the combination of “binary digit”.

Bytes:
A byte consists of 8 bits. 256 cases can be represented by a byte.

In binary, 0000 0000 to 1111 1111. Base 2, which means, two symbols are used here to represent numbers. Computers can understand binary clearly, but the reason why we barely use it in our normal life is that it is too long and not straight-forward for human beings to read and write (I guess).

In decimal, 0 to 255. Base 10. Decimal is the most familiar numeral system for humans. This is probably because of people have 10 fingers.

In hexadecimal, 00 to FF. Base 16, from 0 to 9, and then A, B, C, D, E, F. This is because engineers tend to group bits to make work easier. This is kinda like a human-friendly representation of binary-coded values. Similarly we also use octal to group 3 bits in earlier days.

How many bits does each type in C takes up?

type 32 bit 64 bit x86-64
char 1 1 1
short 2 2 2
int 4 4 4
long 4 8 8
float 4 4 4
double 8 8 8
pointer 4 8 8

A 32-bit system can access 32 bit (2^32) memory address. A 64-bit operating system has 64 bit for memory address. An x86-64 is a 64-bit system over x86 instruction set.

Bit-level manipulations:
&: and. True if both of the two factors are true, false otherwise.

& 0 1
0 0 0
1 0 1

|: or. True if at least one of the two factors is true.

| 0 1
0 0 1
1 1 1

~: not. If A = 1, ~A = 0; if A = 0, ~A = 1;

^: xor. True if and only if one of the two factors is true.

^ 0 1
0 0 1
1 1 0

Bit-level manipulations can be used for bit vectors, sets, “intergral” types in C (char, short, int, long, unsigned)..


References:
1. 15213 Course Home
2. Hexadecimal_numeral_system
All of the articles here are just personal reading notes. Please contact me if you believe there is any copyright infringement.

Python cheat sheet

Binary, Octal, Hexadecimal, Decimal

1
2
3
4
>>> bin(10)
'0b1010'
>>> int('0b1010', 2)
10
1
2
3
4
>>> oct(10)
'012'
>>> int('012', 8)
10
1
2
3
4
>>> hex(10)
'0xa'
>>> int('0xa', 16)
10

Zip in Python

Say I have a list:

1
a = ['.87654321', '2........', '3........', '4........', '5........', '6........', '7........', '8........', '9........']

How can I “transpose” it?

1
b = zip(*a)

then

1
2
>>> b
[('.', '2', '3', '4', '5', '6', '7', '8', '9'), ('8', '.', '.', '.', '.', '.', '.', '.', '.'), ('7', '.', '.', '.', '.', '.', '.', '.', '.'), ('6', '.', '.', '.', '.', '.', '.', '.', '.'), ('5', '.', '.', '.', '.', '.', '.', '.', '.'), ('4', '.', '.', '.', '.', ' ', '.', '.', '.'), ('3', '.', '.', '.', '.', ' ', '.', '.', '.'), ('2', '.', '.', '.', '.', ' ', '.', '.', '.'), ('1', '.', '.', '.', '.', ' ', '.', '.', '.')]

const T& xx

Start on a new page. Following Scratchapixel to review and learn graphics. In the first lesson, a constructor

1
Vec3(const T &xx) : x(xx), y(xx), z(xx) {}

in class Vec3 is used. As a weak learner of c++, I’m confused about the parameter passing here. Why using const T &xx?
An answer on the stack overflow solved my question.

post
What’s the difference?

1
2
void DoWork(int n);
void DoWork(const int &n);

Answer:
The difference is more prominent when you are passing a big struct/class.

1
2
3
4
5
6
struct MyData {
int a,b,c,d,e,f,g,h;
long array[1234];
};
void DoWork(MyData md);
void DoWork(const MyData& md);

When you use use ‘normal’ parameter, you pass the parameter by value and hence creating a copy of the parameter you pass. if you are using const reference, you pass it by reference and the original data is not copied.
In both cases, the original data cannot be modified from inside the function.
EDIT:
In certain cases, the original data might be able to get modified as pointed out by Charles Bailey in his answer.

Speaking of rendering

Speaking of rendering, I will have to mention this genius - “Octane”. As officially introduced, Octane is the world’s first and fastest GPU-accelerated, unbiased, physically correct renderer. It is said that Octane is insanely fast, especially crazy fast SSS, DOF and Motion Blur. What I was amazed is that works made by Octane are always “unrealisticly” realistic(perhaps also because of the artist’s style). It simulates the weather and sky environment throughout 365 days of a year. But, finally, the price is also beautiful, so unfortunately I don’t have the chance to own one except for the very limited trial version. So, now, it’s in my wishlist. I will purchase one once I get a job… (…)

Samples rendered by Octane (Artist: Beeple).