r/learnprogramming • u/abpolus2002 • 12d ago
I want to know more about Error Detections.
Hi, I am new in this community and I am using Python 3.
I have a question about Error Detection. Why are we not using a format like "0xxxxxxx" for 128-255 "10xxxxxx" for 64-127 "110xxxxx" for 32-63 "1110xxxx" for 16-31 "11110xxx" for 8-15 "111110xx" for 4-7 "1111110x" for 2-3 and "1111111x" for 0-1?
This is how it would look from 0 to 255:
def bin_to_int_(s):
return int(s, 2)
def int_to_bin_(s):
return bin(s)[2:]
v_256 = (
[bin_to_int_("0"+int_to_bin_(v)[1:]) for v in range(128, 256)][::-1]+
[bin_to_int_("10"+int_to_bin_(v)[1:]) for v in range(64, 128)][::-1]+
[bin_to_int_("110"+int_to_bin_(v)[1:]) for v in range(32, 64)][::-1]+
[bin_to_int_("1110"+int_to_bin_(v)[1:]) for v in range(16, 32)][::-1]+
[bin_to_int_("11110"+int_to_bin_(v)[1:]) for v in range(8, 16)][::-1]+
[bin_to_int_("111110"+int_to_bin_(v)[1:]) for v in range(4, 8)][::-1]+
[bin_to_int_("1111110"+int_to_bin_(v)[1:]) for v in range(2, 4)][::-1]+
[bin_to_int_("1111111"+int_to_bin_(v)[0:]) for v in range(0, 2)][::-1]
)[::-1]
print(v_256)
If you have an answer please give it a try and publish it, thank you.
22
u/8dot30662386292pow2 12d ago
I have extreme difficulty to understand what you are asking.
4
u/8dot30662386292pow2 12d ago
Are you asking why certain decimal number is certain binary number? Because binary is just base 2 positional numbering system. It is not decided to be like that, it is like that.
And what do you mean by error detection?
3
u/NomaTyx 12d ago
how exactly would this solution help with error detection
2
u/JivanP 12d ago
You have described a scheme which uses 8 bits to represent 256 different values (the numbers from 0 to 255). Since there are 256 possible strings of 8 bits, this does not provide any error detection, because every possible string of 8 bits corresponds to some valid value in your system.
In order to detect an error, there need to be some bitstrings that are not valid values. If a person receives one of these invalid values, they then know that an error occurred and they can then make a judgement call as to what valid value the sender intended to send.
Example: You intend to send the value 4, which in your system would be represented as "11111000". You send it to me, and two bits randomly get flipped during transmission, resulting in me receiving "10111010". This is a valid bitstring in your system; it represents the value 122. So how can I possibly know that you didn't actually mean to send me 122? From my perspective as the recipient, the outcome is identical in both of these cases:
13
u/ILMTitan 12d ago
The scheme you are describing would not detect errors. Every single bit pattern would still map to a valid integer. But the way it would map would be much harder to build efficient arithmetic processers for.
We don't do error detection/correcting on ints. It is much more efficient to error correct larger chunks of data, and often the hardware will do error correction for us so we don't have to think about it. See ECC memory.
6
u/da_Aresinger 12d ago edited 12d ago
encoding isn't error detection. Error detection comes after encoding.
You also didn't provide an example of how you would expect to use this error detection, lwt alone how it wohld work.
4
u/petros211 12d ago
Bruh what? What are you trying to do? What even is the question? Use this format to represent what? How does this relate to error detection? Which errors are you trying to detect?
4
u/TurnipEvening273 12d ago
All the bits in your "format" is consumed to actually carry the information about the "integar" it supposedly carries. For whatever data we need to "detect errors" in, it has to be wrapped in an external carrier or a "container" which specifies the bits carrying necessary information to "detect" or may even "fix" the errors. Those bits / information required to detect the errors, cannot come from the data itself. Well, in some cases it may seem 🤔 that the data itself carries the means of error detection, but actually that disqualifies it from "being" carried message.
A better way to understand is to first realize that the information to be carried has to be pure/stochastic. And only then can we decide the means to detect errors. In your case the "format" fails this criteria because the bits you modify, are actually required to know the format itself.
A question to ask first is "how will the receiver know the format the integar is packed with?"
1
u/AcornAnomaly 12d ago
What kind of error detection?
Do you mean in just arbitrary data, like transmission errors?
Or do you mean some sort of programming error?
2
u/Amuro_Ray 12d ago
How would this detect an error like a missing parameter in a method?
1
u/phatbili 12d ago
Think of it like a password where every possible combination of letters is accepted. If you make a typo you just log into somebody else's account instead of getting an error message.
1
u/defectivetoaster1 12d ago
the point of an error detecting code is to encode data such the receiver can notice if something isn’t right. Naively, you could just double every bit eg 0110 would map to 00111100, that way if there’s ever a bit that doesn’t occur twice in a row the receiver knows that something flipped. This of course works (assuming both bits in a pair don’t flip) but for every data bit you’ve added a redundancy bit which is wasteful. Using a parity bit solves that problem by XORing every bit in a frame and generating one more bit from that. If there’s an even number of 1s in the data then the parity bit is 0 and if there’s an odd number then the parity bit is 1. The receiver can check the data and compare it to the parity bit, if there’s a mismatch due to an odd number of bit flips this will be caught.
Error correcting codes (eg hamming codes) go one step further by being able to detect and also correct errors. The way they do this is by mapping the space of unencoded data to a space with larger word sizes. since it’s a one to one mapping of discrete values, there’s necessarily going to be some words in the new space that don’t correspond to words in the original space (eg if your mapping just appended a 0 at the end, then any encoded word ending in a 1 doesn’t correspond to a valid data word). By choosing the mapping well you can say that some of the invalid words have a high probability of being caused by bit flips from a valid word, and hamming codes do this in such a way that maximises the hamming distance between valid words in the encoded space.
Crucially, all these methods rely on having redundant bits, so not all the words in the encoded space directly correspond to valid data. Your encoding maps every encoded word directly to valid data so it provides no error detection let alone correction
1
u/LochsideArb 7d ago
That specific bit pattern layout isn't standard for error detection in most protocols unless you're building something custom. Usually, we just use checksums or CRC32 to keep data integrity simple.
0
u/abpolus2002 12d ago
I just want to use it in my projects and I need to know advantages and disadvantages, if my topic is misleading I am sorry because I thought it would be like parity bits but it can determine what possible number it is.