c++ - 从缓冲区输入的位

标签 c++ c types binary bit

包含缓冲区值的文件。前 16 位包含类型。接下来的 32 位给出了数据的长度。数据中的剩余值。

如何从16位中查找类型(查找是int还是char...)

我在这里陷入了我的思考过程。无法找到将位转换为类型的方法。

最佳答案

假设您有家庭作业:

You are given a file where the first bit encodes the type, the next 7 bits encode the length, and the rest is the data.

The types are encoded in the following way:

  • 0 is for int
  • 1 is for char

Print the ints or chars separated by newlines.

您只需使用给定的信息即可!由于使用 1 位来对类型进行编码,因此有两种可能的类型。所以你只需阅读第一位,然后执行:

if (bit == 0) {
    int *i = ...
}
else if (bit == 1) {
    char *c = ...
}

关于c++ - 从缓冲区输入的位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54971680/

相关文章:

c++ - C++ (03) SFINAE 方面编译器是否独立?

c - 以 sprintf 科学记数法打印所有有效数字

c++ - 在 C++ 代码中使用 K&R 风格的 C 函数

javascript - JavaScript 数组初始化之前的 "var"指的是什么,是列表的元素还是数组本身?

c++ - const 引用的类型是什么?

c++ - 为什么带有 switch 语句的函数不需要 return

c++ - 如何为想要公开相互递归函数的仅 header 库组织包含?

c++ - a, &a for int[][] 的区别

c++ - 是否可以重载模板类?

c - 即使我在主内存中还有很多空间,realloc 也会失败