Negatives and Complements

What are complements ? Complements are way to store negative numbers and manipulate them in binary. Why complements ? Representing negative numbers in binary was a real hassle ! That’s were complements help to store signed numbers as binary. But it was not always like that. 1. Sign-Magnitude First idea of representing signs: 0 = + and 1 = -, followed by the number in binary ! 00000101 = 5 10000101 = -5 ...

July 5, 2025

Dangers Using Reinterpret_cast

Several dangers using reinterpret_cast<> Reinterpreting smaller data pointer to bigger data pointers Reinterpreting pointers of smaller data like char to a pointer of bigger data like int will cause issues like reading from unreserved memory, reading garbage values and other “bad” stuff. For example: char ch = 'A'; char* c_ptr = &ch; int* i_ptr = reinterpret_cast<int*>(&ch); //this is "BAD" ❌ std::cout << *i_ptr << std::endl; //ch is 1 byte but trying to access it using a int* will read 4 bytes and even though the whole thing will contain our char it will still have garbage values. //int is 4 bytes so, //byte1(our character) ~ byte2(??) ~ byte3(??) ~ byte4(??) // read^(*i_ptr) Does the garbage value contain our character ? YES ! It will ! ...

July 5, 2025

Reinterpret_Cast

What is reinterpret cast<> ? Just like type casting (char)int, which reinterprets the value numerically here: int x = 65; std::cout << (char)x << std::endl; //prints character 'A' as 65 is the ascii code for 'A'. //OR std::cout << static_cast<char>(x) << std::endl; //C++ style Similarly reinterpret casts works on pointers ! Specific type of pointers can read specific type of data. Means a int pointer will read 4 bytes of data at a time whereas a char pointer reads 1 byte of data at a time. ...

July 5, 2025

Deep understanding of Pointers and Pointer pitfall

Pointers are easy ? Or are they ? Pointers are pretty easy to use and understand unless we fall in deep dereferencing and more complex topics like array of pointers and multiple dereferences and incrementing or decrementing them. Basic Pointer use: int a = 3; int* ptr = &a; std::cout << a << std::endl; //prints value of a std::cout << &a << std::endl; //prints address of a std::cout << ptr << std::endl; //also prints address of a std::cout << *ptr << std::endl; //prints value of a Now lets go a little deep using pointer to a pointer. ...

July 5, 2025