int x = 2;
x = rotateInt('L', x, 1); // should return 4
x = rotateInt('R', x, 3); // should return 64
Run Code Online (Sandbox Code Playgroud)
这是代码,有人可以检查它,让我知道错误是什么?
编译成功,但它说Segmentation Fault我执行它.
int rotateInt(char direction, unsigned int x, int y)
{
int i;
for(i = 0; i < y; i++)
{
if(direction == 'R')
{
if((x & 1) == 1)
{
x = x >> 1;
x = (x ^ 128);
}
else
x = x >> 1;
}
else if(direction == 'L')
{
if((x …Run Code Online (Sandbox Code Playgroud) 可能重复:
何时在C#中使用Shift运算符<< >>?
我编程了一段时间,我从未使用过移位运算符.我可以看到它如何有助于计算像in这样的哈希码Tuple<T>,但除此之外,
使字典<Int64,byte>得到了很多使用.我的意思是在大数据负载中运行数天的循环.Int64来自两个Int32.该字节恰好是来自许多很长列表的那两个Int32之间的距离(计数).
我在这个循环中需要做的是
现在我使用直接数学来生成密钥,我知道有更快的方法,但我无法弄明白.我把shift作为标签,因为我认为这是如何优化它,但我无法弄明白.
然后当循环完成时,我需要从Int64中提取两个Int32以将数据插入到数据库中.
谢谢
每个注释我使用的数学将两个Int32组合成一个Int64
Int64 BigInt;
Debug.WriteLine(Int32.MaxValue);
Int32 IntA = 0;
Int32 IntB = 1;
BigInt = ((Int64)IntA * Int32.MaxValue) + IntB;
Debug.WriteLine(BigInt.ToString());
IntA = 1;
IntB = 0;
BigInt = ((Int64)IntA * Int32.MaxValue) + IntB;
Debug.WriteLine(BigInt.ToString());
IntA = 1;
IntB = 1;
BigInt = ((Int64)IntA * Int32.MaxValue) + IntB;
Debug.WriteLine(BigInt.ToString());
Run Code Online (Sandbox Code Playgroud)
最好的密钥可能不是Int64.我所拥有的是两个Int32,它们共同形成一个键.和一个字节的值.我需要快速查找该复合键.字典很快但它不支持复合键,因此我创建了一个实际上是复合键的单个键.在SQL Int32A中,Int32B形成PK.
我不使用复合键的原因是我希望字典的查找速度和我的知识字典不支持复合键.这是生产代码.在SQL表中实际上有第三个键(Int32 sID,Int32 IntA,Int32 IntB).在这个解析器中,我一次只处理一个sID(并按顺序处理sID).我开始使用SQL的复合键查找(运行中数十亿).当我拉出IntA时,IntB输出到Dictionary以处理单个sID,然后在每个sID完成时加载到SQL我获得了100:1的性能提升.部分性能改进是插入,因为当我从字典插入时我可以按PK顺序插入.新的IntA和IntB不是按解析方式生成的,因此直接插入SQL会严重破坏索引,我需要在运行结束时重建索引.
我试图做一个更长的功能,但它的行为非常奇怪.我试图找出问题所在,我已经找到了这个有缺陷的部分.
这个程序是为Arduino制作的,但这种现象可能出现在其他环境中.我试图进行大量搜索,但我找不到解决方案.
为什么这两个代码不能给出相同的结果?
如何在没有额外变量的情况下构建单行函数,
但是像"代码1"一样操作?
结果:
源代码:
代码1 :(正确操作,但不是一行)
#include <binary.h>
const byte value=B00110110;
byte buffer,result;
void setup(){
Serial.begin(115200);
buffer = (value << 3);
result = (buffer >> 2);
Serial.println(result,BIN);
}
void loop(){
}
Run Code Online (Sandbox Code Playgroud)
它给出:0b00101100
代码2 :(不正确的操作,但一行)
#include <binary.h>
const byte value=B00110110;
byte result;
void setup(){
Serial.begin(115200);
result = ((value << 3) >> 2);
Serial.println(result,BIN);
}
void loop(){
}
Run Code Online (Sandbox Code Playgroud)
它给出:0b01101100
我正在尝试实现一个函数,用于将对象数组移动到数组的右侧.我在互联网上找到的所有内容都是循环移位的实现,但这不是我想要的.如果theres实际上是空的,那么我想将元素向右移动.假设您创建了一个对象Packet数组,其大小为10
Packet* a[] = { p4 , p3 , p2 , p1, null, null, null, null, null, null }
Run Code Online (Sandbox Code Playgroud)
换挡功能只会将一切都移到右边
{ null ,p4 , p3 , p2 , p1, null, null, null, null, null }
Run Code Online (Sandbox Code Playgroud)
并且在阵列末尾有一个元素的情况下
{ p10, p9, p8, p7 ,p6 ,p5 ,p4 , p3 , p2 , p1}
Run Code Online (Sandbox Code Playgroud)
这个功能不会改变任何东西.
{ p4 , p3 , p2 , p1, null, null, null, null, null, null }
Run Code Online (Sandbox Code Playgroud)
我的实现的想法是将数组复制到临时数组,擦除原始数组上的所有内容,然后复制到它,但从位置[1]而不是位置[0]开始.但这似乎不是很有效.
还有其他想法吗?
我正在学习Perl并且正在尝试编写一个脚本,该脚本将模式和文件列表作为命令行参数并将它们传递给子例程,然后子例程打开每个文件并打印与模式匹配的行.以下代码有效; 但是,它在从第一个文件打印行后停止,甚至没有触摸第二个文件.我在这里错过了什么?
#!/usr/bin/perl
use strict;
use warnings;
sub grep_file
{
my $pattern = shift;
my @files = shift;
foreach my $doc (@files)
{
open FILE, $doc;
while (my $line = <FILE>)
{
if ($line =~ m/$pattern/)
{
print $line;
}
}
}
grep_file @ARGV;
Run Code Online (Sandbox Code Playgroud) 这是一个例子:
$a = shift;
$b = shift;
push(@ARGV,$b);
$c = <>;
print "\$b: $b\n";
print "\$c: $c\n";
print "\$ARGV: $ARGV\n";
print "\@ARGV: @ARGV\n";
Run Code Online (Sandbox Code Playgroud)
并输出:
$b: file1
$c: dir3
$ARGV: file2
@ARGV: file3 file1
Run Code Online (Sandbox Code Playgroud)
我不明白在没有任何索引的情况下打印$ ARGV时究竟发生了什么.它是否打印第一个参数然后从数组中删除它?因为我认为在所有语句之后数组变成:
file2 file3 file1
Run Code Online (Sandbox Code Playgroud)
调用:
perl port.pl -axt file1 file2 file3
Run Code Online (Sandbox Code Playgroud)
file1包含以下行:
dir1
dir2
Run Code Online (Sandbox Code Playgroud)
文件2:
dir3
dir4
dir5
Run Code Online (Sandbox Code Playgroud)
文件3:
dir6
dir7
Run Code Online (Sandbox Code Playgroud) 我想知道这是否可能......
我知道255 = 11111111如果我做255 << pos*8并且pos = 1我会有1111111100000000.
我想知道是否有可能获得值1111111111111111左移而不做cicle而不是为了.
谢谢.
我想要一个根据输入索引u移动的函数.如果你是负面的,向右移动,否则向左移动.使用下面的代码得到的结果deque与输入相同.
deque<float> move(deque<float>p, int u)
{
if(u==0 || p.size()==1)
return p;
else if(u<0)
{
for(int i=0; i<abs(p.size()); i++)
{
int temp = p.back();
p.pop_back();
p.push_front(temp);
}
}
else
{
for(int i=0; i<p.size(); i++)
{
int temp = p.front();
p.pop_front();
p.push_back(temp);
}
}
return p;
}
Run Code Online (Sandbox Code Playgroud)
这段代码的另一个变体似乎在Python中运行良好,但在C++中却不行,这是:
deque<float> move1(deque<float>p, int u)
{
deque<float> q;
for(int i=0; i<p.size(); i++)
q.push_back(p[(i-u) % p.size()]);
return q;
}
Run Code Online (Sandbox Code Playgroud) 我想知道如何在C++中改变左边的值.例如:
1 << 180
Run Code Online (Sandbox Code Playgroud)
我认为结果应该是:
1532495540865888858358347027150309183618739122183602176
Run Code Online (Sandbox Code Playgroud)
(在python中测试[1 << 180]);