Short solutions of 2014

Back to Blog

In this section I will collect short things that have taken me some time to figure out. The intent is that I would be able to avoid wasting time in the future after I have forgotten about them.

PyPi upload fails

11.12.2014

I upgraded my operating system a few days ago. When trying to upload a new version of Remark into PyPi, there was the following error:

Upload failed (401): You must be identified to edit package information.

This error was resolved by “python setup.py register”, where I entered my username and password again.

Incorrect end-of-lines prohibit Python script execution

05.12.2014

If a Python script contains a shebang and has Windows end-of-lines (CR-LF), then the script will not execute. The solution is to convert the end-of-lines to Unix (LF), for example with dos2unix.

Bitwise operators in C++

03.02.2014

Bitwise operators in C++ offer many traps for portability. This seems unnecessary painful when implementing a data-structure based on the w-bit RAM model. Ideally, I would like to see the standard define the shifts portably as

x >> k = floor(x / 2^k)
x << k = mod(x 2^k, 2^w)

Unfortunately, that’s not the case.

If one uses a signed integer, then the results are well-defined for binary or, binary and, binary xor, and binary not, but depend on the representation of signed numbers that the compiler (processor) uses. Some representations include ones’ complement, two’s complement, or signed-magnitude. For each one, the result is different, and thus the bitwise operators on signed integers are not portable for the values.

Shifts of negative signed integers are implementation-defined in C++, and so unportable, although they wouldn’t need to be. Even worse, if there are n bits in an integer, then shifting an integer, even an unsigned integer, by n or more bits is implementation-defined. This latter thing on unsigned integer has caused real bugs with me.

Thus, to be portable, one should always work with non-negative integers when using bitwise operators, and even then be careful with the shift operators. Related, the number of bits in an integer can be computed with

sizeof(Type) * CHAR_BIT

where CHAR_BIT is a constant defined in <climits>. While CHAR_BIT is 8 in many systems, that is not always true. However, it is true that CHAR_BIT is at least 8, by the minimal range requirements the standard imposes on chars.

__sincos_stret

27.01.2014

When trying to link Pastel in Matlab 2013a in Mac OS X, I was faced with a linker error which stated that it could not find the function __sincos_stret. A little bit of googling revealed that this was because of the upgrade to the OS X Maverics. To solve this problem, I