- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我创建了一个算法,用于将最大 255 字节的整数除以一个 8 位整数,并且它适用于我完成的测试。有没有人对此有任何意见或改进建议?为此有更好的算法吗?我不想通过 bignum 除法算法得到 bignum,第二个整数是一个 8 位整数。
迄今为止的最佳解决方案(小端):
typedef struct{
u_int8_t * data;
u_int8_t length;
}CBBigInt;
void CBBigIntEqualsDivisionByUInt8(CBBigInt * a,u_int8_t b,u_int8_t * ans){
// base-256 long division.
u_int16_t temp = 0;
for (u_int8_t x = a->length-1;; x--) {
temp <<= 8;
temp |= a->data[x];
ans[x] = temp / b;
temp -= ans[x] * b;
if (!x)
break;
}
a->length -= ans[a->length-1]? 0 : 1; // If last byte is zero, adjust length.
memmove(a->data, ans, a->length); // Done calculation. Move ans to "a".
}
大端的旧解决方案:
它的工作原理是:
typedef struct{
u_int8_t * data;
u_int8_t length;
}CBBigInt;
u_int8_t CBPowerOf2Log2(u_int8_t a){
switch (a) {
case 1:
return 0;
case 2:
return 1;
case 4:
return 2;
case 8:
return 3;
case 16:
return 4;
case 32:
return 5;
case 64:
return 6;
}
return 7;
}
u_int8_t CBFloorLog2(u_int8_t a){
if (a < 16){
if (a < 4) {
if (a == 1){
return 0;
}
return 1;
}
if (a < 8){
return 2;
}
return 3;
}
if (a < 64){
if (a < 32) {
return 4;
}
return 5;
}
if (a < 128){
return 6;
}
return 7;
}
void CBBigIntEqualsRightShiftByUInt8(CBBigInt * a,u_int8_t b){
u_int8_t deadBytes = b / 8; // These bytes fall off the side.
a->length -= deadBytes; // Reduce length of bignum by the removed bytes
u_int8_t remainderShift = b % 8;
if (!remainderShift) { // No more work
return;
}
u_int16_t splitter;
u_int8_t toRight = 0; // Bits taken from the left to the next byte.
for (u_int8_t x = 0; x < a->length; x++) {
splitter = a->data[x] << 8 - remainderShift; // Splits data in splitters between first and second byte.
a->data[x] = splitter >> 8; // First byte in splitter is the new data.
a->data[x] |= toRight; // Take the bits from the left
toRight = splitter; // Second byte is the data going to the right from this byte.
}
}
void CBBigIntEqualsDivisionByUInt8(CBBigInt * a,u_int8_t b,u_int8_t * ans){
if (!(b & (b - 1))){
// For powers of two, division can be done through bit shifts.
CBBigIntEqualsRightShiftByUInt8(a,CBPowerOf2Log2(b));
return;
}
// Determine how many times b will fit into a as a power of two and repeat for the remainders
u_int8_t begin = 0; // Begining of CBBigInt in calculations
bool continuing = true;
u_int8_t leftMost;
bool first = true;
while (continuing){
// How much does b have to be shifted by before it becomes larger than a? Complete the shift into a shiftedByte
int16_t shiftAmount;
u_int16_t shiftedByte;
if (a->data[begin] > b){
shiftAmount = CBFloorLog2(a->data[begin]/b);
shiftedByte = b << 8 + shiftAmount;
}else if (a->data[begin] < b){
shiftAmount = -CBFloorLog2(b/a->data[begin]);
shiftedByte = b << 8 + shiftAmount;
// Shift right once again if "shiftedByte > (a->data[begin] << 8) + a->data[begin+1]" as the shifted divisor should be smaller
if (shiftedByte > ((a->data[begin] << 8) + a->data[begin+1])){
shiftedByte >>= 1;
shiftAmount--; // Do not forget about changing "shiftAmount" for calculations
}
}else{
shiftAmount = 0;
shiftedByte = b << 8;
}
// Set bit on "ans"
if (shiftAmount < 0){ // If "shiftAmount" is negative then the byte moves right.
ans[begin+1] |= 1 << (8 + shiftAmount);
if (first) leftMost = 1;
}else{
ans[begin] |= 1 << shiftAmount; // No movement to right byte, jsut shift bit into place.
if (first) leftMost = 0;
}
first = false; // Do not set "leftMost" from here on
// Take away the shifted byte to give the remainder
u_int16_t sub = (a->data[begin] << 8) + a->data[begin+1] - shiftedByte;
a->data[begin] = sub >> 8;
if (begin != a->length - 1)
a->data[begin + 1] = sub; // Move second byte into next data byte if exists.
// Move along "begin" to byte with more data
for (u_int8_t x = begin;; x++){
if (a->data[x]){
if (x == a->length - 1)
// Last byte
if (a->data[x] < b){
// b can fit no more
continuing = false;
break;
}
begin = x;
break;
}
if (x == a->length - 1){
continuing = false; // No more data
break;
}
}
}
a->length -= leftMost; // If the first bit was onto the 2nd byte then the length is less one
memmove(a->data, ans + leftMost, a->length); // Done calculation. Move ans to "a".
}
谢谢!
最佳答案
我将其描述为“base 2 long division”。更好的选择是“base 256 长除法”。
这是一个(未经测试且可能有错误的)示例:
typedef struct{
u_int8_t * data;
u_int8_t length;
} CBBigInt;
u_int8_t CBBigIntEqualsDivisionByUInt8(CBBigInt * a, u_int8_t b, u_int8_t * ans) {
int i;
unsigned int temp = 0;
i = a.length;
while(i > 0) {
i--;
temp <<= 8;
temp |= a.data[i];
ans.data[i] = temp / b;
temp -= ans.data[i] * b;
}
return temp; // Return remainder
}
关于c - 无符号 8 位整数的 Bignum 除法。 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10522379/
给定一个字符串,例如 s="##$$$#",我如何找到索引之前的“#”符号数等于“”数的索引$"符号在索引之后? 示例:如果 s="##$$$#",则输出将为 2。 解释:在索引 2 之前我们有 2
在本教程中,您将借助示例了解 JavaScript 符号。 JavaScript 符号 JavaScript ES6 引入了一种新的原始数据类型,称为 Symbol(符号)。符号是不可变的(不能更改)
在“函数编程的工艺”一书中,符号 '>.>' 将函数连接在一起,与 '.' 的方向相反。但是当我使用 ghci 实现它时,它显示了超出范围的错误 '>.>'。为什么?它是不再使用的旧符号吗? 最佳答案
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我需要从向量中删除 \"。这是我的数据: data <- c("\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1
我在 Nginx 配置中使用正则表达式来捕获文件 URL,但如果文件 URL 包含 # 符号,正则表达式模式将不会捕获它。 这里是nginx的配置部分。 location ~ ^/p/(?[\w\-=
如何使 & 符号在此图表的第一组条形/列下正确显示: http://jsfiddle.net/VxbrK/2/ 应该是“Apples & Oranges”而不是“Apples & Oranges”。
**在verilog中是什么意思? 我为测试台提供了以下逻辑 localparam NUM_INPUT_BITS = 1; localparam NUM_OUTPUT_BITS
我有一个使用正则表达式来验证电子邮件地址的方法。 public String searchFormail(String searchWord) { Pattern pattern = Patt
我想将一个字符串拆分为数字部分和文本/符号部分我当前的代码不包含负数或小数,并且表现得很奇怪,在输出的末尾添加了一个空列表元素 import re mystring = 'AD%5(6ag 0.33-
我有一些代码需要从数组中选择一个随机字符串,但它一直返回单个字母或数字。如何解决这个问题? var name = ["Yayek", "Vozarut", "Gezex",
我刚开始使用 Python,我在考虑应该使用哪种表示法。我读过 PEP 8关于 Python 符号的指南,我同意那里的大多数内容,除了函数名称(我更喜欢混合大小写风格)。 在 C++ 中,我使用匈牙利
在用 C# 编写代码时,我错误地在 if 语句中的变量前添加了一个符号(而不是感叹号)。 bool b = false; if (@b) { } 我很惊讶它编译成功,没有任何错误。 我想知道:上面的代
本文实例为大家分享了特殊字符替换电话号码中某一部分的方法,ios利用-号替换电话号码中间四位,供大家参考,具体内容如下 1、效果图 2、代码 rootviewcontroll
当我使用“x”和“z”作为符号时,这段代码没有问题: from sympy import * x, z = symbols('x z') y = -6*x**2 + 2*x*z**0.5 + 50*x
我需要从文本中删除标点符号: data <- "Type the command AT&W enter. in order to save the new protocol on modem;"
我有几个数字是 numeric 类。下面的例子。 df = c(12974,12412,124124,124124,34543,4576547,32235) 现在我想在每个数字前添加 '$' 符号而不
我有一个 highcharts 图例,其中符号以不同的大小显示,因为它们在实际图表中的大小不同。不幸的是,当数据点的大小增加时,它们也会在图例中增加。无论数据点大小如何,我都希望图例符号保持相同的大小
我需要使用包含平均值+-SD的标题。到目前为止,我只能得到以下信息: "Mean +- SD or N (%)" [1] "Mean +- SD or N (%)" 如何直接使用“+-”符号?您知道一
使用 XSLT 和 XPath 1.0,我有一个要转义的字符串以用于 URL,例如: one word & another 因此,描述元素的 text() 应该进行 URL 转义。 我该怎么做
我是一名优秀的程序员,十分优秀!