将一个整数按原样存到内存中去…
本帖最后由 cnjn 于 2019-6-8 08:39 编辑现在有一个不超过6位数的非负整数,比如15
存到内存中应该就变成了0x0f,占用 sizeof(int) 个字节
可我想让它变成{0x15,0x00,0x00},占用3字节
对应的,
23-->{0x23,0x00,0x00},占用3字节
3456-->{0x34,0x56,0x00},占用3字节
256366-->{0x25,0x63,0x66},占用3字节
这要怎样才能做到?有大佬能指点一下吗? 不行呢,要是你这样存到内存里面去,谁知道怎么取出来?到底是一个字节还是两个字节?还是三个字节?规定大小是有意义的 本帖最后由 cnjn 于 2019-6-8 08:18 编辑
Krant5 发表于 2019-6-8 02:35
不行呢,要是你这样存到内存里面去,谁知道怎么取出来?到底是一个字节还是两个字节?还是三个字节?规定大小是 ...
嗯……可以规定为3个字节,不足的用00填充 Krant5 发表于 2019-6-8 02:35
不行呢,要是你这样存到内存里面去,谁知道怎么取出来?到底是一个字节还是两个字节?还是三个字节?规定大小是 ...
谢谢提醒,我有添加了一些限制条件 可以直接分配3字节内存,然后把你要存的数字两两拆开,在依次存入每个字节里面去 #include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef struct {
uint32_t original;
uint8_t *data;
size_t size;
} number_t;
number_t *number_init(uint32_t num)
{
uint8_t buff; // uint32_t 最大4294967295
size_t index = 0;
uint32_t original = num;
while(num)
{
buff = num % 10;
num /= 10;
}
number_t *number = malloc(sizeof(number_t));
number->data = malloc((index + 1) / 2);
number->size = 0;
number->original = original;
for(size_t i = 0; i < index;)
{
uint8_t temp = buff;
if(i < index)
temp |= (buff) << 4;
number->data = temp;
}
return number;
}
void number_deinit(number_t *number)
{
free(number->data);
free(number);
}
void number_print(const number_t *number)
{
printf("%u: ", number->original);
for(size_t i = 0; i < number->size; ++i)
printf("0x%x ", number->data);
if(number->original == 0)
printf("0x%x", 0);
printf("\n");
}
number_t *number_add(const number_t *a, const number_t *b)
{
return number_init(a->original + b->original);
}
number_t *number_sub(const number_t *a, const number_t *b)
{
return number_init(a->original - b->original);
}
number_t *number_mul(const number_t *a, const number_t *b)
{
return number_init(a->original * b->original);
}
number_t *number_div(const number_t *a, const number_t *b)
{
return number_init(a->original / b->original);
}
int main(void)
{
number_t *number;
number = number_init(4294967295);
number_print(number);
number_deinit(number);
number = number_init(429496729);
number_print(number);
number_deinit(number);
number = number_init(42949672);
number_print(number);
number_deinit(number);
number = number_init(429);
number_print(number);
number_deinit(number);
number = number_init(42);
number_print(number);
number_deinit(number);
number = number_init(4);
number_print(number);
number_deinit(number);
number = number_init(0);
number_print(number);
number_deinit(number);
number_t *a = number_init(12);
number_t *b = number_init(3);
number_t *sum = number_add(a, b);
number_t *sub = number_sub(a, b);
number_t *mul = number_mul(a, b);
number_t *div = number_div(a, b);
printf("a -> "); number_print(a);
printf("b -> "); number_print(b);
printf("sum -> "); number_print(sum);
printf("sub -> "); number_print(sub);
printf("mul -> "); number_print(mul);
printf("div -> "); number_print(div);
number_deinit(a);
number_deinit(b);
number_deinit(sum);
number_deinit(sub);
number_deinit(mul);
number_deinit(div);
return 0;
}
本帖最后由 人造人 于 2019-6-9 13:57 编辑
对0进行测试
我知道不能除0,问题是这个除0错误应该由哪一层来处理,如何处理?如果我不管,操作系统肯定会管
那么我该如何管?提示用户说,0不能做除数,然后退出程序?
问题是操作系统就是这样做的
对,我不应该对操作系统处理除0错误做任何假设,问题是如果不这样做,还能如何做呢?^_^
我不想把问题搞复杂,把除0错误留给操作系统处理
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef struct {
uint32_t original;
uint8_t *data;
size_t size;
} number_t;
number_t *number_init(uint32_t num)
{
uint8_t buff; // uint32_t 最大4294967295
size_t index = 0;
uint32_t original = num;
while(num)
{
buff = num % 10;
num /= 10;
}
number_t *number = malloc(sizeof(number_t));
number->data = malloc((index + 1) / 2);
number->size = 0;
number->original = original;
for(size_t i = 0; i < index;)
{
uint8_t temp = buff;
if(i < index)
temp |= (buff) << 4;
number->data = temp;
}
return number;
}
void number_deinit(number_t *number)
{
free(number->data);
free(number);
}
void number_print(const number_t *number)
{
printf("%u: ", number->original);
for(size_t i = 0; i < number->size; ++i)
printf("0x%x ", number->data);
if(number->original == 0)
printf("0x%x", 0);
printf("\n");
}
number_t *number_add(const number_t *a, const number_t *b)
{
return number_init(a->original + b->original);
}
number_t *number_sub(const number_t *a, const number_t *b)
{
return number_init(a->original - b->original);
}
number_t *number_mul(const number_t *a, const number_t *b)
{
return number_init(a->original * b->original);
}
number_t *number_div(const number_t *a, const number_t *b)
{
return number_init(a->original / b->original);
}
int main(void)
{
number_t *a = number_init(12);
number_t *b = number_init(0);
number_t *sum = number_add(a, b);
number_t *sub = number_sub(a, b);
number_t *mul = number_mul(a, b);
number_t *div = number_div(a, b);
printf("a -> "); number_print(a);
printf("b -> "); number_print(b);
printf("sum -> "); number_print(sum);
printf("sub -> "); number_print(sub);
printf("mul -> "); number_print(mul);
printf("div -> "); number_print(div);
number_deinit(a);
number_deinit(b);
number_deinit(sum);
number_deinit(sub);
number_deinit(mul);
number_deinit(div);
return 0;
}
本帖最后由 人造人 于 2019-6-9 13:57 编辑
被除数为0
然后,我在研究为什么 0 - 12 = 4294967284
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef struct {
uint32_t original;
uint8_t *data;
size_t size;
} number_t;
number_t *number_init(uint32_t num)
{
uint8_t buff; // uint32_t 最大4294967295
size_t index = 0;
uint32_t original = num;
while(num)
{
buff = num % 10;
num /= 10;
}
number_t *number = malloc(sizeof(number_t));
number->data = malloc((index + 1) / 2);
number->size = 0;
number->original = original;
for(size_t i = 0; i < index;)
{
uint8_t temp = buff;
if(i < index)
temp |= (buff) << 4;
number->data = temp;
}
return number;
}
void number_deinit(number_t *number)
{
free(number->data);
free(number);
}
void number_print(const number_t *number)
{
printf("%u: ", number->original);
for(size_t i = 0; i < number->size; ++i)
printf("0x%x ", number->data);
if(number->original == 0)
printf("0x%x", 0);
printf("\n");
}
number_t *number_add(const number_t *a, const number_t *b)
{
return number_init(a->original + b->original);
}
number_t *number_sub(const number_t *a, const number_t *b)
{
return number_init(a->original - b->original);
}
number_t *number_mul(const number_t *a, const number_t *b)
{
return number_init(a->original * b->original);
}
number_t *number_div(const number_t *a, const number_t *b)
{
return number_init(a->original / b->original);
}
int main(void)
{
number_t *a = number_init(0);
number_t *b = number_init(12);
number_t *sum = number_add(a, b);
number_t *sub = number_sub(a, b);
number_t *mul = number_mul(a, b);
number_t *div = number_div(a, b);
printf("a -> "); number_print(a);
printf("b -> "); number_print(b);
printf("sum -> "); number_print(sum);
printf("sub -> "); number_print(sub);
printf("mul -> "); number_print(mul);
printf("div -> "); number_print(div);
number_deinit(a);
number_deinit(b);
number_deinit(sum);
number_deinit(sub);
number_deinit(mul);
number_deinit(div);
return 0;
}
a -> 0: 0x0
b -> 12: 0x12
sum -> 12: 0x12
sub -> 4294967284: 0x84 0x72 0x96 0x94 0x42
mul -> 0: 0x0
div -> 0: 0x0
0 - 12 在硬件层计算出的结果是 0xfffffff4,对于有符号数来说就是 -12
我不知道为什么printf以%u输出会被解释为4294967284,如果改为%x会输出fffffff4
我的研究到此为止
因为我在初始化时保存了原值,省略了转换的逆运算,我想这样应该没办法说服你,主要是无法说服我,^_^
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef struct {
uint8_t *data;
size_t size;
} number_t;
number_t *number_init(uint32_t num)
{
uint8_t buff; // uint32_t 最大4294967295
size_t index = 0;
while(num)
{
buff = num % 10;
num /= 10;
}
number_t *number = malloc(sizeof(number_t));
number->data = malloc((index + 1) / 2);
number->size = 0;
for(size_t i = 0; i < index;)
{
uint8_t temp = buff;
if(i < index)
temp |= (buff) << 4;
number->data = temp;
}
return number;
}
void number_deinit(number_t *number)
{
free(number->data);
free(number);
}
uint32_t number_original(const number_t *number)
{
uint32_t original = 0;
for(ssize_t i = number->size - 1; i >= 0; --i)
{
original = original * 10 + (number->data >> 4);
original = original * 10 + (number->data & 0x0f);
}
return original;
}
void number_print(const number_t *number)
{
uint32_t original = number_original(number);
printf("%u: ", original);
for(size_t i = 0; i < number->size; ++i)
printf("0x%x ", number->data);
if(original == 0)
printf("0x%x", 0);
printf("\n");
}
number_t *number_add(const number_t *a, const number_t *b)
{
return number_init(number_original(a) + number_original(b));
}
number_t *number_sub(const number_t *a, const number_t *b)
{
return number_init(number_original(a) - number_original(b));
}
number_t *number_mul(const number_t *a, const number_t *b)
{
return number_init(number_original(a) * number_original(b));
}
number_t *number_div(const number_t *a, const number_t *b)
{
return number_init(number_original(a) / number_original(b));
}
int main(void)
{
number_t *number;
number = number_init(4294967295);
printf("%u\n", number_original(number));
number_deinit(number);
number = number_init(429496729);
printf("%u\n", number_original(number));
number_deinit(number);
number = number_init(42949672);
printf("%u\n", number_original(number));
number_deinit(number);
number = number_init(429);
printf("%u\n", number_original(number));
number_deinit(number);
number = number_init(42);
printf("%u\n", number_original(number));
number_deinit(number);
number = number_init(4);
printf("%u\n", number_original(number));
number_deinit(number);
number = number_init(0);
printf("%u\n", number_original(number));
number_deinit(number);
number_t *a = number_init(12);
number_t *b = number_init(3);
number_t *sum = number_add(a, b);
number_t *sub = number_sub(a, b);
number_t *mul = number_mul(a, b);
number_t *div = number_div(a, b);
printf("a -> "); number_print(a);
printf("b -> "); number_print(b);
printf("sum -> "); number_print(sum);
printf("sub -> "); number_print(sub);
printf("mul -> "); number_print(mul);
printf("div -> "); number_print(div);
number_deinit(a);
number_deinit(b);
number_deinit(sum);
number_deinit(sub);
number_deinit(mul);
number_deinit(div);
return 0;
}
你这等于新建一个数据类型,这种要求拿c++比较容易实现class tri_int {
private:
unsigned char _nums;
public:
tri_int() {
memset(_nums, 0, 3);
}
tri_int(unsigned int val):tri_int() {
for (int i = 0;val!=0 && i < 3; ++i) {
_nums = val % 100;
val /= 100;
}
}
operator unsigned int() {
return _nums + 100 * _nums + 10000 * _nums;
}
};
这个tri_int 就是满足你要求的类型 Croper 发表于 2019-6-9 15:48
你这等于新建一个数据类型,这种要求拿c++比较容易实现
这个tri_int 就是满足你要求的类型
可是。。。还没学c++诶。。。{:10_266:} 人造人 发表于 2019-6-9 14:20
0 - 12 在硬件层计算出的结果是 0xfffffff4,对于有符号数来说就是 -12
我不知道为什么printf以%u输出会被 ...
%u输出的就是无符号数啊。。。即使-12本身是有符号数也会被当作无符号吧 人造人 发表于 2019-6-9 14:20
0 - 12 在硬件层计算出的结果是 0xfffffff4,对于有符号数来说就是 -12
我不知道为什么printf以%u输出会被 ...
2^64 - 12 = 4294967284
-12的补码被当成无符号数时就会变成4294967284的,也就是2^64-12 cnjn 发表于 2019-6-23 14:39
2^64 - 12 = 4294967284
-12的补码被当成无符号数时就会变成4294967284的,也就是2^64-12
嗯
页:
[1]