首页 文章

C - 有符号和无符号整数

提问于
浏览
0

我正在深入研究C,因为我需要将ctypes库导入python以允许键盘控制 . 我正在尝试了解以下代码的工作原理:

import ctypes
import time

SendInput = ctypes.windll.user32.SendInput

# C struct redefinitions 
PUL = ctypes.POINTER(ctypes.c_ulong)
class KeyBdInput(ctypes.Structure):
    _fields_ = [("wVk", ctypes.c_ushort),
                ("wScan", ctypes.c_ushort),
                ("dwFlags", ctypes.c_ulong),
                ("time", ctypes.c_ulong),
                ("dwExtraInfo", PUL)]

class HardwareInput(ctypes.Structure):
    _fields_ = [("uMsg", ctypes.c_ulong),
                ("wParamL", ctypes.c_short),
                ("wParamH", ctypes.c_ushort)]

class MouseInput(ctypes.Structure):
    _fields_ = [("dx", ctypes.c_long),
                ("dy", ctypes.c_long),
                ("mouseData", ctypes.c_ulong),
                ("dwFlags", ctypes.c_ulong),
                ("time",ctypes.c_ulong),
                ("dwExtraInfo", PUL)]

class Input_I(ctypes.Union):
    _fields_ = [("ki", KeyBdInput),
                 ("mi", MouseInput),
                 ("hi", HardwareInput)]

class Input(ctypes.Structure):
_fields_ = [("type", ctypes.c_ulong),
            ("ii", Input_I)]

# Actuals Functions

def PressKey(hexKeyCode):

    extra = ctypes.c_ulong(0)
    ii_ = Input_I()
    ii_.ki = KeyBdInput( hexKeyCode, 0x48, 0, 0, ctypes.pointer(extra) )
    x = Input( ctypes.c_ulong(1), ii_ )
    SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))

def ReleaseKey(hexKeyCode):

    extra = ctypes.c_ulong(0)
    ii_ = Input_I()
    ii_.ki = KeyBdInput( hexKeyCode, 0x48, 0x0002, 0, ctypes.pointer(extra) )
    x = Input( ctypes.c_ulong(1), ii_ )
    SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))


def AltTab():
    '''
    Press Alt+Tab and hold Alt key for 2 seconds in order to see the overlay
    '''

    PressKey(0x012) #Alt
    PressKey(0x09) #Tab
    ReleaseKey(0x09) #~Tab

    time.sleep(2)       
    ReleaseKey(0x012) #~Alt


if __name__ =="__main__":

    AltTab()

我不理解的部分与有符号和无符号整数有关:

int的范围是-32768 - 32767

unsigned int的范围是0 - 65535

我读到:“2字节数字可以显示的总数字范围是2 ^ 16,因为你有16位可以表示一个数字.2 ^ 16与65536相同,因为我们从0开始计算,与0 - 65535相同 . 这显然与unsigned int的值匹配,因此您可以看到这就是该类型的运行方式 . “

这似乎有道理,但有一件事我不明白:

1字节= 8位2字节= 16位

那么为什么一个2字节的数字被称为2 ^ 16而不是2 ^ 8?

3 回答

  • 1

    我不理解的部分与有符号和无符号整数有关:int的范围是-32768 - 32767 unsigned int的范围是0 - 65535

    仅供参考: int 的大小(因此它可以容纳的值)实际上可能取决于您的环境 . 确定整数变量类型(至少从C99开始)的大小(位宽)的确定方法是使用 stdint.h 中定义的类型之一,在那里你会发现像 int8_tint16_t 这样的显式类型 . 通常不需要,但对于一个学习C来说只是一个有趣的事实 .

    无论如何,关于你的问题 . 所以“为什么一个2字节的数字被称为2 ^ 16而不是2 ^ 8”

    16是比特数 . 在2个字节中有16位 .

    2表示每个位的可能性 . (0或1)

    因此,2个字节可以表示从00000000000000002到11111111111111112的第2 ^ 16个数字

  • 1

    2字节数具有16位(2×8位) . 告诉您可以由给定位数表示的最高无符号数的函数是 2^n-1 ,因此例如8位可以表示数字0到255,16位0到65,535等 .

    这样做的原因是简单的 . 考虑第一个不能用(例如)16位表示的数字 . 这将是1,有16个零,因为's the smallest binary number with 17 digits. That' s 2^16 . 因此,可以用这种方式表示的最大数字是 2^16-1 .

    另请注意,C中 int 的大小取决于您的C编译器 . 它可能不总是2个字节长 .

  • 1

    2字节数包含16位 . 它们中的每一个都可以容纳2个值,因此它可以生成2 ^ 16个可能的数字,这是您可以编码的不同信息的数量 .

相关问题