鉴于以下代码片段:
signed char x = 150;
unsigned char y = 150;
printf("%d %d\n", x, y);
Run Code Online (Sandbox Code Playgroud)
输出是:
-106 150
Run Code Online (Sandbox Code Playgroud)
但是,对于以相同方式在内存中表示的变量,我使用了相同的格式说明符。printf 如何知道它是有符号的还是无符号的。
两种情况下的内存表示为:
10010110
Run Code Online (Sandbox Code Playgroud) 我有两个char值char_1和char_2.现在我想将它们组合成一个16位有符号整数值,其中char_1包含MSB中的符号.
| SGN | Bit 6 | Bit 5 | Bit 4 | Bit 3 | Bit 2 | Bit 1 | Bit 0 | Bit 7 | Bit 6 | Bit 5 | Bit 4 | Bit 3 | Bit 2 | Bit 1 | Bit 0 |
| 签署字符1 | 其余的Char 1 | Char 2 |
我的尝试是:
signed short s = (((int)char_1) << 8) & (int)char_2;
Run Code Online (Sandbox Code Playgroud)
现在我得到0 s...
有人可以解释一下Golang中的左/右移位行为.请参考以下示例代码:https://play.golang.org/p/7vjwCbOEkw
package main
import (
"fmt"
)
func main() {
var lf int8 = -3
fmt.Printf("-3 : %08b\n", lf)
fmt.Printf("<<1: %08b\n", lf<<1)
fmt.Printf("<<2: %08b\n", lf<<2)
fmt.Printf("<<3: %08b\n", lf<<3)
fmt.Printf("<<4: %08b\n", lf<<4)
fmt.Printf("<<5: %08b, %d\n", lf<<5, lf<<5)
fmt.Printf("<<6: %08b, %d\n", lf<<6, lf<<6)
fmt.Printf("<<7: %08b, %d\n", lf<<7, lf<<7)
fmt.Printf("<<8: %08b, %d\n", lf<<8, lf<<8)
fmt.Printf("<<9: %08b, %d\n", lf<<9, lf<<9)
}
-3 : -0000011
<<1: -0000110
<<2: -0001100
<<3: -0011000
<<4: -0110000
<<5: -1100000, -96
<<6: 01000000, 64
<<7: -10000000, -128 …Run Code Online (Sandbox Code Playgroud) 这可能是一个非常基本的问题,但我无法做到。这是我正在使用的。
#include <stdio.h>
int main(void)
{
char c1, c2;
int s;
c1 = 128;
c2 = -128;
s = sizeof(char);
printf("size of char: %d\n", s);
printf("c1: %x, c2: %x\n", c1, c2);
printf("true or false: %d\n", c1 == c2);
}
Run Code Online (Sandbox Code Playgroud)
结果是这样的。
size of char: 1
c1: ffffff80, c2: ffffff80
true or false: 1
Run Code Online (Sandbox Code Playgroud)
我将值 128 分配给有符号(正常)字符类型,但它没有溢出。
此外,c1 和 c2 似乎都持有 4 个字节,并且 -128 和 128 是相同的值。
我如何理解这些事实?我需要你的帮助。非常感谢。
在计算机编程中,存在正负零.这可能不会让你感到震惊,但奇怪的是,正零等于负零的明显事实.这是一个例子:
public static class Program
{
public static void Main()
{
int positive = 0;
int negative = -0;
Console.WriteLine(positive == negative);
Console.Read();
}
}
Run Code Online (Sandbox Code Playgroud)
输出显示为"True".
至于每个人显然充斥着"Int默认为0"的评论.我应该指出,浮动和双打做同样的事情,在负和正零之间的平等中返回真实.
我想这可能与图形的概念有关,其中负零点和正零点将在同一维度点内正交存在.还是他们之间存在平等的另一个原因是什么?