我在C中有这样的代码:
unsigned char const data[ ] = {0x0a, 0x1d, 0xf0, 0x07};
Run Code Online (Sandbox Code Playgroud)
我需要提取它,使得最终值为:
0xa1df7
Run Code Online (Sandbox Code Playgroud)
如果十六进制值至少为零,我只能提取并使其工作:
unsigned char const data[ ] = {0x0a, 0xd0, 0xf0, 0x07};
Run Code Online (Sandbox Code Playgroud)
使用以下代码:
for(int i = 0; i < SIZE; ++i)
{
tmp = data[i];
if ( (data[i] <= 0x0F) && (((data[i] & 0x0F) == 0) || (data[i] & 0xF0) == 0)) // one of the hex is zero
{
tmp = ((tmp << 4) >> 4) << N[i];
std::cout << "foo: " << std::hex << tmp << …Run Code Online (Sandbox Code Playgroud) def different(s):
x = len(s)
for i in range(1, 1 << x):
u.append([s[j] for j in range(x) if (i & (1 << j))])
Run Code Online (Sandbox Code Playgroud)
它需要一个列表并进行不同的组合(a,b,c)=((a,b,c),(a,b),(a,c)等.)但是这个范围是做什么的?从1到什么.我不明白"<<"
而且,如果(i&(1 << j))这是做什么的?它会检查i和2是否具有j的功率?对我没有任何意义:/ /
unsigned int command = 4;
cout << command;
command = (command << 1);
cout << command;
command = (command << 1);
cout << command;
Run Code Online (Sandbox Code Playgroud)
输出:
4
8
10
Run Code Online (Sandbox Code Playgroud)
为什么是最后一行的输出10,而不是16?
我认为移位运算符会移动整数的内存或应用它的字符,但以下代码的输出对我来说是一个惊喜.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main(void) {
uint64_t number = 33550336;
unsigned char *p = (unsigned char *)&number;
size_t i;
for (i=0; i < sizeof number; ++i)
printf("%02x ", p[i]);
printf("\n");
//shift operation
number = number<<4;
p = (unsigned char *)&number;
for (i=0; i < sizeof number; ++i)
printf("%02x ", p[i]);
printf("\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它运行的系统是little endian并产生以下输出:
00 f0 ff 01 00 00 00 00
00 00 ff 1f 00 00 00 00
Run Code Online (Sandbox Code Playgroud)
有人可以提供一些参考变速操作员的详细工作吗?
第一:
for i in range(4):
return (1 << i)
Run Code Online (Sandbox Code Playgroud)
第二个:
for i in range(4):
return (2^i)
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么2之间存在差异?
我试图找出什么
>> 3
Run Code Online (Sandbox Code Playgroud)
在下面显示的代码中.是>>重定向,如果是,那么什么是3?有人可以帮忙吗?
#define BYTESIZE(bitsize) ((bitsize + 7) >> 3)
Run Code Online (Sandbox Code Playgroud) 我想检查一些大的计算内存需求(存储在一个unsigned long long)将与用于编译我的代码的内存模型大致兼容.
我假设当需要通过指针中的位数向右移位时将导致0,当且仅当存储器需要适合虚拟地址空间时(与实际操作系统限制无关).
不幸的是,在一些编译器上将64位数字移位64位时,我发现了一些意想不到的结果.
小演示:
const int ubits = sizeof (unsigned)*8; // number of bits, assuming 8 per byte
const int ullbits = sizeof (unsigned long long)*8;
cout << ubits << " bits for an unsigned\n";
cout << ullbits << " bits for a unsigned long long \n";
unsigned utest=numeric_limits<unsigned>::max(); // some big numbers
unsigned long long ulltest=numeric_limits<unsigned long long>::max();
cout << "unsigned "<<utest << " rshift by " << ubits << " = "
<< (utest>>ubits)<<endl; …Run Code Online (Sandbox Code Playgroud) 在C&C++中,如果右操作数在使用>>和<<(右移和左移操作符)时为负,则程序的行为未定义.考虑以下计划:
#include <iostream>
int main()
{
int s(9);
std::cout<<(s<<-3);
}
Run Code Online (Sandbox Code Playgroud)
g ++给出以下警告:
[Warning] left shift count is negative [enabled by default]
Run Code Online (Sandbox Code Playgroud)
MSVS 2010发出以下警告:
warning c4293: '<<' : shift count negative or too big, undefined behavior
Run Code Online (Sandbox Code Playgroud)
现在我好奇Java和C#会发生什么?
我试过以下程序
class left_shift_nagative
{
public static void main(String args[])
{
int a=3;
System.out.println(a<<-3);
System.out.println(a>>-3);
}
}
Run Code Online (Sandbox Code Playgroud)
计划成果:
1610612736
0
Run Code Online (Sandbox Code Playgroud)
C#轮到:
namespace left_shift_nagative
{
class Program
{
static void Main(string[] args)
{
int s = 3;
Console.WriteLine(s << -3);
Console.WriteLine(s >> …Run Code Online (Sandbox Code Playgroud) 我试图将逻辑右移(>>>)(>>>和>>之间的差异)转换为C#的Java代码
Java代码是
return hash >>> 24 ^ hash & 0xFFFFFF;
Run Code Online (Sandbox Code Playgroud)
C#被标记>>>为语法错误.
如何解决?
更新1
人们建议>>在C#中使用,但它没有解决问题.
System.out.println("hash 1 !!! = " + (-986417464>>>24));
Run Code Online (Sandbox Code Playgroud)
是197年
但
Console.WriteLine("hash 1 !!! = " + (-986417464 >> 24));
Run Code Online (Sandbox Code Playgroud)
是-59
谢谢!
为什么3右移32等于3而不是0。
我在nodeJs和Java中得到了这些结果