为了提高我们公司的数据安全性,我们计划开始使用灰色代码编号(其中连续值仅相差一位),而不是传统的二进制数字。
请帮助我们制作一个程序,将数字转换为灰色代码(单个数字到单个数字),但是程序也需要安全。这意味着程序的每个连续字节最多只能相差一位。
下面是来自维基百科的代码,用于转换(未加高尔夫球和“不安全”):
uint BinaryToGray(uint num)
{
return num ^ (num >> 1); // The operator >> is shift right. The operator ^ is exclusive or.
}此代码执行以下操作:输入编号向右移动一位,然后执行排他性或原始编号。输入123的示例:
二进制表示123为1111011b,移位后右移后的数字为0111101b。在执行排他性或这两个数字之间的结果是1000110 b,这是小数70。
输入、输出实例:
0, 0
1, 1
123, 70
255, 128
11, 14安全程序示例:
a
bcbcbc
!!!
h()
bcg不安全方案的例子:
abc
OP发布于 2022-06-20 12:43:32
;3ss{Ƶ}]\\^;^对核心方案的解释:
; # Halve the (implicit) input
^ # Implicitly truncate it to an integer, and bitwise-XOR it with the
# (implicit) input
# (after which the result is output implicitly)这样做的目的是在不改变功能的情况下在它们之间添加任何操作。我是从创建一个生成器来获取所有与单个位不同的05AB1E字符对.开始的。在^之前,只有\和V看起来比较合理(\放弃堆栈的顶部;V弹出堆栈的顶部,并将其保存在变量Y中)。从那开始,我就试着去看看我能用什么角色。因为;和^有4位不同,所以至少在两者之间有很多没有操作的字节是必需的。虽然我觉得字节计数可能还可以改进,但我想,切换4个特定位的9个无操作字节并不是太糟糕。
对新方案的解释:
; # Halve the (implicit) input-integer
# No-ops:
3 # Push 3
s # Swap the two values on the stack
s # Swap them back
{ # Sort the digit(s) of 3
Ƶ} # Push compressed integer 226
] # Close all open if-statements and loops
\ # Discard the top of the stack (the 226)
\ # Discard the top again (the 3)
^ # Implicitly truncate the halved input to an integer, and bitwise-XOR
# it with the (implicit) input
# (after which the result is output implicitly)(参见这个05AB1E提示(一节)如何压缩大整数?)来理解为什么Ƶ}是226。)
05AB1E代码页中的字节 of ;3ss{Ƶ}]\\^是:
; 00111011
v
3 00110011
v
s 01110011
s 01110011
v
{ 01111011
v
Ƶ 01111111
v
} 01111101
v
] 01011101
v
\ 01011100
\ 01011100
v
^ 01011110https://codegolf.stackexchange.com/questions/248809
复制相似问题