HEVC 标准中的 HEVC 量化(统一情况)步骤需要在计算电平系数时右移 QP/6。
对于 QP 不能被 6 整除的情况,我不确定如何执行这种右移。任何帮助将不胜感激。
参考:高效视频编码 (HEVC) 标准中的核心转换设计:Madhukar Budagavi,IEEE 高级成员,Arild Fuldseth,Gisle Bjøntegaard,Vivienne Sze,IEEE 成员和 Mangesh Sadafale
有没有一种简单的方法可以使左移和减法像在 C 中那样使用 Cython 对无符号整数进行操作?
例如:
def left_shift(unsigned int x, unsigned int shift):
return x << shift
def main():
print left_shift(0xffffffff, 4)
print left_shift(0xffffffff, 8)
print left_shift(0xffffffff, 12)
Run Code Online (Sandbox Code Playgroud)
我希望这能打印十进制等价物
0xfffffff0
0xffffff00
0xfffff000
Run Code Online (Sandbox Code Playgroud)
这实际上就是我得到的。
4294967280
4294967040
4294963200
Run Code Online (Sandbox Code Playgroud)
然而,如果我尝试做一些更复杂的事情,例如在大输入上使用 Jenkins 的哈希函数之一,这就是我得到的:
def hash_fcn1(unsigned int key):
key = (key ^ 0xdeadbeef) + (key << 4)
key = key ^ (key >> 10)
key = key + (key << 7)
key = key ^ (key >> 13)
return key
hash_fcn1(0xffffffff)
File "./hash_fcn_test.py", line 94, …Run Code Online (Sandbox Code Playgroud) 我想测试按位运算是否真的比算术运算更快.我以为他们是.
我写了一个小的C程序来测试这个假设,令我惊讶的是,加法平均比按位AND运算少.这对我来说是令人惊讶的,我无法理解为什么会这样.
根据我所知的附加,来自较低有效位的进位应该被携带到下一位,因为结果也取决于进位.对我来说逻辑运算符比加法更慢是没有意义的.
我的鳕鱼在下面:
#include<stdio.h>
#include<time.h>
int main()
{
int x=10;
int y=25;
int z=x+y;
printf("Sum of x+y = %i", z);
time_t start = clock();
for(int i=0;i<100000;i++)z=x+y;
time_t stop = clock();
printf("\n\nArithmetic instructions take: %d",stop-start);
start = clock();
for(int i=0;i<100000;i++)z=x&y;
stop = clock();
printf("\n\nLogic instructions take: %d",stop-start);
}
Run Code Online (Sandbox Code Playgroud)
一些结果:
Arithmetic instructions take: 327
Logic instructions take: 360
Arithmetic instructions take: 271
Logic instructions take: 271
Arithmetic instructions take: 287
Logic instructions take: 294
Arithmetic instructions take: 279
Logic instructions take: …Run Code Online (Sandbox Code Playgroud) c assembly instructions logical-operators integer-arithmetic
我写了这样的代码:
#include<iostream>
using namespace std;
int main()
{
cout<<static_cast<float>(5/9)*9;
cout<<static_cast<float>(5)/9*9;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
预期产量:55
原始输出:05
为什么第一个静态转换语句变成 0 ?
假设我定义了两个变量x和y:
int x = 5;
int y = 6;
Run Code Online (Sandbox Code Playgroud)
我可以x通过以下方式获取存储地址:
cout << &x << endl; // 0x61fe18
Run Code Online (Sandbox Code Playgroud)
这是一个十六进制数。现在,如果我想在加上 5 后打印这个数字,这很简单:
cout << &x + 5 << endl; // 0x61fe1d
Run Code Online (Sandbox Code Playgroud)
但是,如果我总结的地址一起x和y:
cout << &x + &y << endl;
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
Run Code Online (Sandbox Code Playgroud)invalid operands of types 'int*' and 'int*' to binary 'operator+' cout << &x + &y << endl; ~~~^~~~
为什么我不能添加这两个地址?
让我们检查Add并Multiply作为例子。哪一种可以归类为加宽?
假设输入是2 的补码中的有符号字符(即长度为 8 位),除非另有声明。
1+2 = 3
Run Code Online (Sandbox Code Playgroud)
这个操作似乎没有扩大。1、2 和 3 都适合一个字符。
但是,250 + 6溢出无符号字符。那么这是在扩大吗?
同样可以用有符号类型完成,将有符号125 + 5字符溢出到符号位中。这是在扩大吗?
-2-3 = -5
Run Code Online (Sandbox Code Playgroud)
这会使二进制字符溢出 1 位:
1 1 1 1 1 1 1 0
+ 1 1 1 1 1 1 0 1
------------------
1 1 1 1 1 1 0 1 1
Run Code Online (Sandbox Code Playgroud)
溢出通常会被丢弃,但是,这是否被视为扩大操作?
1 * 2 = 2
Run Code Online (Sandbox Code Playgroud)
即使结果仍然适合原始数据类型,所有乘法是否都在扩大?
上面的例子2仍然适合一个 8 位字符,但是,如果我用二进制手工进行数学运算,额外的0s会附加到结果的左侧,结果被丢弃。 …
assembly cpu-architecture twos-complement integer-arithmetic
我在 C 中发现了一种奇怪的(对我来说)行为。int如果这是一个基本问题,请注意,但我无法找到为什么以下代码会产生意外结果的答案。
#include <stdio.h>
int main(void)
{
printf("1000 * 0.1 = %d\n", (1000 * 0.1));
printf("1000 * (10/100) = %d\n", (1000 * (10/100)));
printf("(int)1000 * 0.1 = %d\n", (int)(1000 * 0.1));
printf("(int)1000 * (10/100) = %d\n", (int)(1000 * (10/100)));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
两者的结果是-O0相同-O3的:
1000 * 0.1 = -957043896
1000 * (10/100) = 0
(int)1000 * 0.1 = 100
(int)1000 * (10/100) = 0
Run Code Online (Sandbox Code Playgroud)
我预计前两个结果是无意义的(我不知道为什么,但我预计将 double 传递给 int 参数不应该起作用)。然而3和4之间的差异让我感到困惑。我期望(10/100)在编译时计算并呈现与 3 …
c floating-point printf integer-arithmetic conversion-specifier
假设一个有8个无符号字符x1,x2,...x8,我们想要计算:
abs((x1 + x2 + x3 + x4) - (x5 + x6 + x7 + x8)) / 4
Run Code Online (Sandbox Code Playgroud)
在不引入大量上溢或下溢错误的情况下,确保最准确结果的最佳方法是什么?
我在模板类中使用它,这就是为什么我不能将无符号值转换为有符号值.
有没有一种更好的方法可以在不同的原始类型(使用自动升级)上进行算术运算,而不是显式的转换和展开?
例如,如果喜欢:
let a: u8 = 1;
let b: u16 = 2;
let c: u32 = 3;
Run Code Online (Sandbox Code Playgroud)
我可以以某种方式摆脱所有演员阵容:
let total: u64 = a.to_u64().unwrap() + b.to_u64().unwrap() + c.to_u64().unwrap();
Run Code Online (Sandbox Code Playgroud) 如果不使用'<'运算符,你将如何递归地编写一个检查数字是否小于另一个的方法?
x并且y将始终为0或更大booleanCove我到目前为止:
public static boolean isLessThan(int x, int y) {
if(x == y - 1) return true;
if(x == y + 1) return false;
if(x == y) return false;
return isLessThan((x), (y-1)) || isLessThan((x-1), y);
}
Run Code Online (Sandbox Code Playgroud)