Why is 1 Byte equal to 8 Bits? 

I’ts been a minute since I took computer organization, but the relevant wiki on ‘Byte’ gives some context. The byte was originally the smallest number of bits that could hold a single character (I assume standard ASCII). We still use ASCII standard, so 8 bits per character is still relevant. This sentence, for instance, is 41 bytes. … Read more

Swapping 2 Bytes of Integer

Couple of things You can recombine by masking x with a value that is all FF except for bytes m and n You can compute the mask by left shifting 0xFF m times and n times and combining the result and then XOR it with 0xFFFFFFFF FYI when you right shift a signed value, it … Read more

MySQL Error #1071 – Specified key was too long; max key length is 767 bytes

767 bytes in MySQL version 5.6 (and prior versions), is the stated prefix limitation for InnoDB tables. It’s 1,000 bytes long for MyISAM tables. This limit has been increased to 3072 bytes In MySQL version 5.7 (and upwards). You also have to be aware that if you set an index on a big char or varchar field which is utf8mb4 encoded, you have to … Read more

How do I initialize a byte array in Java?

You can use an utility function to convert from the familiar hexa string to a byte[]. When used to define a final static constant, the performance cost is irrelevant. Since Java 17 There’s now java.util.HexFormat which lets you do This utility class lets you specify a format which is handy if you find other formats easier to read or when … Read more

What is the difference between a string and a byte string?

Assuming Python 3 (in Python 2, this difference is a little less well-defined) – a string is a sequence of characters, ie unicode codepoints; these are an abstract concept, and can’t be directly stored on disk. A byte string is a sequence of, unsurprisingly, bytes – things that can be stored on disk. The mapping between them is … Read more

Does sizeof return the number of bytes or the number of octets of a type in C?

Answer: sizeof returns the size of the type in bytes. Example: sizeof(char) is 100% guaranteed to be 1, but this does not mean, that it’s one octet (8 bits). Proved by the standard: in 6.5.3.4, point 2: The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The … Read more

Convert bytes to int?

Assuming you’re on at least 3.2, there’s a built in for this: int.from_bytes( bytes, byteorder, *, signed=False ) … The argument bytes must either be a bytes-like object or an iterable producing bytes. The byteorder argument determines the byte order used to represent the integer. If byteorder is “big”, the most significant byte is at the beginning of the byte array. If byteorder is “little”, the most significant byte is … Read more

How many bits is a “word”?

I’m not familiar with either of these books, but the second is closer to current reality. The first may be discussing a specific processor. Processors have been made with quite a variety of word sizes, not always a multiple of 8. The 8086 and 8087 processors used 16 bit words, and it’s likely this is … Read more