temp1=temp1>>1
时间: 2024-06-17 16:07:59 浏览: 96
temp1=temp1>>1是C++中的位运算,意思是将temp1向右移动1位,即将temp1的二进制数表示向右移动一位。这个操作会将temp1的二进制数表示的每一位向右移动一位,并在左边补0。这个操作相当于将temp1除以2取整,因为二进制中向右移动一位相当于除以2取整。这个操作可以用来对二进制数进行快速的除以2操作。
相关问题
用python语言翻译如下代码://线性移位寄存器生成密钥 int IFSR(int K) { int feature = 143; int temp1, temp2; temp1 = feature & K; while (temp1) { temp2 = (temp2 + temp1 & 1) % 2; temp1 = temp1 >> 1; } K = ((K << 1) & 255) + temp2; return K; } //生成16轮密钥 void getKeys(int round) { for (int i = 0; i < round; i++) { int temp = 0; for (int j = 0; j < 8; j++) { K = IFSR(K); temp += K & 1; temp = temp << 1; } temp = temp >> 1; Keys[i] = temp; } } //轮函数 int F(int RE, int Ki) { Ki = Ki >> 4; int temp = Ki ^ RE; return temp; } //明文加密 void encrypy(int round) { int m, temp1, temp_j; int le, re; m = strlen(str_plaincode); for (int i = 0; i < m; i++) { int temp1; if (i == 0) temp1 = IV; else temp1 = ((IV << j) ^ temp_j) & 255; le = temp1 >> 4; re = temp1 & 15; for (int j = 0; j < round; j++) { int temp2 = le; le = re; re = (temp2 ^ F(re, Keys[j])); } temp_j = (re << 4) + le; temp_j = temp_j >> (8 - j); str_ciphertext[i] = str_plaincode[i] ^ temp_j; } } //解密
以下是Python代码的翻译:
```python
# 线性移位寄存器生成密钥
def IFSR(K):
feature = 143
temp1, temp2 = 0, 0
temp1 = feature & K
while temp1:
temp2 = (temp2 + temp1 & 1) % 2
temp1 >>= 1
K = ((K << 1) & 255) + temp2
return K
# 生成16轮密钥
def getKeys(round):
global Keys
Keys = [0] * round
for i in range(round):
temp = 0
for j in range(8):
global K
K = IFSR(K)
temp += K & 1
temp <<= 1
temp >>= 1
Keys[i] = temp
# 轮函数
def F(RE, Ki):
Ki = Ki >> 4
temp = Ki ^ RE
return temp
# 明文加密
def encrypy(round):
global str_ciphertext
m = len(str_plaincode)
for i in range(m):
temp1 = 0
if i == 0:
temp1 = IV
else:
temp1 = ((IV << j) ^ temp_j) & 255
le = temp1 >> 4
re = temp1 & 15
for j in range(round):
temp2 = le
le = re
re = (temp2 ^ F(re, Keys[j]))
temp_j = (re << 4) + le
temp_j = temp_j >> (8 - j)
str_ciphertext.append(str_plaincode[i] ^ temp_j)
# 解密
def decode():
global test
m = len(str_ciphertext)
for i in range(m):
temp1 = 0
if i == 0:
temp1 = IV
else:
temp1 = ((IV << j) ^ temp_j) & 255
le = temp1 >> 4
re = temp1 & 15
for j in range(round):
temp2 = le
le = re
re = (temp2 ^ F(re, Keys[round-j-1]))
temp_j = (re << 4) + le
temp_j = temp_j >> (8 - j)
test += chr(str_ciphertext[i] ^ temp_j)
```
struct node* insert(struct node* head, int newData, int targetData) { struct node *temp, *p, *q; p = NULL; // 创建一个节点p p = (struct node*) malloc (sizeof(struct node)); p ->data = newData; p->next = NULL; // 查找存储targetData的节点 temp = head ; while (temp != NULL) { if (temp ->data == targetData){ return head;} temp = temp -> next;} temp = head; while (temp != NULL) { printf("%d ", temp->data); temp = temp->next; } printf("\n"); return head; } // 如果找到存储targetData的节点 if (temp != NULL) { // 将节点p插入到链表存储targetData节点temp的后面 p -> data = newData; p -> next = temp -> next; temp -> next = p; } return head; }
在insert函数中,查找存储targetData的节点的逻辑存在问题。如果找到该节点,则直接返回原链表的头指针head,没有进行插入操作。如果没有找到该节点,则会遍历整个链表并输出链表的值。正确的逻辑应该是,如果找到存储targetData的节点,则将新节点插入到该节点的后面;如果没有找到该节点,则返回原链表的头指针head,不进行插入操作。
以下是修正后的代码:
```
struct node* insert(struct node* head, int newData, int targetData) {
struct node *temp = head, *p;
p = (struct node*)malloc(sizeof(struct node));
p->data = newData;
p->next = NULL;
while (temp != NULL) {
if (temp->data == targetData) { // 找到存储targetData的节点
p->next = temp->next; // 将节点p插入到存储targetData节点temp的后面
temp->next = p;
return head;
}
temp = temp->next;
}
// 没有找到存储targetData的节点
return head;
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)