简体   繁体   English

汉明码:奇偶校验位数

[英]Hamming Code: Number of parity bits

I'm trying to write a method in java that will take an input of any number of 0 or 1 digits and output that line after being encoded with Hamming Code. 我正在尝试用Java编写一个方法,该方法将接受任意数量的0或1位数字的输入,并在用汉明代码编码后输出该行。

I have managed to write the code when knowing the number of digits the input will have (in this case 16) because knowing the number of digits in the input, I immediately know the number of parity bits there have to be added (5 in this case) to a total of 21 digits in the final output. 当知道输入的位数(在本例中为16)时,我设法编写了代码,因为知道输入中的位数,我立即知道必须添加奇偶校验位的数量(本例中为5)最终输出中总共21位)。 I am working with int arrays so I need to declare a size in the beginning and my code works based on those exact sizes. 我正在使用int数组,因此我需要在开始时声明一个大小,并且我的代码将基于这些确切的大小工作。

Can you guys think of any way/algorithm that can give me the number of digits the output will have (after adding the relevant parity digits to the number of input digits) based solely on the number of input digits? 你们能想到仅凭输入位数就能给我输出的位数(将相关的奇偶校验位加到输入位数后)的任何方式/算法吗?

Or do I have to tackle this problem in a totally different way? 还是我必须以完全不同的方式解决这个问题? Any suggestions? 有什么建议么? Thank you in advance! 先感谢您!

Cheers! 干杯!

Is your input a String or individual bits? 您输入的是字符串还是单个位? If you input as a String, you can convert each character to a bit, and the length of the String gives you the length of the array. 如果输入为字符串,则可以将每个字符转换为一位,并且字符串的长度为您提供数组的长度。

If you need to input the bits one at a time, store them in an ArrayList. 如果需要一次输入一位,请将它们存储在ArrayList中。 When all bits have been entered, you can convert your list to an array easily, or use the size of the list etc. 输入所有位后,您可以轻松地将列表转换为数组,或使用列表的大小等。

From my understanding, you get your 6th parity bit at 32 bits of input, 7th at 64, etc. so what you need is floor(lg(n)) + 1 , which in java you can get by using 32 - Integer.numberOfLeadingZeros(n) . 据我了解,您在输入的32位上获得了第六个奇偶校验位,在64位上获得了第七个奇偶校验位,依此类推,所以您需要的是floor(lg(n)) + 1 ,在Java中,您可以使用32 - Integer.numberOfLeadingZeros(n)

Assuming your input is made up entirely of 0s and 1s, you would do 假设您的输入完全由0和1组成,您将

int parityDigits = 32 - Integer.numberOfLeadingZeros(input.length());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM