方法的位标志参数如何工作(Ansi C/C)?
How does bit flag parameters of a method works (Ansi C/C)?
我正在用 C (ANSI C) 编写 POS(销售点)
我有这个功能
GetString(uchar *str,uchar mode,uchar minlen,uchar maxlen)
有点像 readln
但在 POS
在 API 中,mode
参数类似于 D1、D2、D3...
但在示例中(API)我有这个
if(!GetString(buf, 0x26, 0, 10))
{
buf[buf[0]+1]=0; amt=atol(buf+1);
} else {
/* user press CANCEL button */ }
那么0x26(函数中的参数mode
)和
二进制数或位标志,甚至,我不知道,十六进制。
在 API 中还有另一件事解释了 mode
输入参数
1. Input mode definition: (priority order is bit3>bit4>bit5, and mode&0x38≠0);
2. When inputting, the digit will be displayed on the screen in turns as plaintext or cryptograph (according to bit3).
3. The initial cursor position is determined by ScrGotoxy(x, y).
4. If bit7 of mode =1, then this function could bring initial digit string, and the string is displayed on initial cursor position as input digit string.
5. Output string does not record and contain function keys.
6. Press CLEAR button, if it is plaintext display, then CLEAR is considered as BACKSPACE key; if it is cryptograph display, then CLEAR is considered as the key to clear all contents.
7. Use function key to switch to Capital mode. S80 uses Alpha key to select the corresponding character on a key, however SP30 uses UP and Down key, and T52 uses ―#‖ key, T620 uses key F2.
8. In MT30, the switch method between uppercase, lowercase and number characters is to keep pressing
D1、D2、D3 ... D7 是位。我想它被用作一个位标志。因为它只有 1 个字节,所以它有 8 种可能的状态,所有这些状态都可以组合在一起。
0x26
是十进制38
还是二进制
00100110
这意味着 D1、D2、D5 已设置,其他所有 D 均未设置。
在 page you linked 中指定了 8 位标志。在您的示例中,您的十六进制值 0x26
是二进制值 00100110
。这指定了 8 个 bit-flags,其中 3 个(D1、D2、D5)已设置,5 个(D0、D3、D4、D6、D7)已清除。
如果你参考你的 table 链接(它是一个图形所以我不能粘贴它),它会告诉你 GetString
参数 mode
如何指示函数的行为, 对于 8 bit-flags 中的每一个设置 (1) 或清除 (0).
例如D2
设置为1表示左对齐.
组合各个标志得到一个二进制数,在您的示例中,它作为十六进制数传递 0x26
。
这对你有帮助,我定义了一个宏来操作 D7 位,根据文档是 ENTER 模式的位。
以相同的方式继续其他模式。
// bits manipulation
// idx stand for bit index
#define CHECK_BIT(var,idx) ((var >> idx) & 1)
#define SET_BIT(var,idx,n) (var ^= (-(n?1:0) ^ var) & (1 << idx))
// helping macros
// check if Enter mode is set D7 is used
#define IS_ENTER_MODE_SET(mode) CHECK_BIT(mode,7)
// set Enter mode to opt (true,false)
#define SET_ENTER_MODE (mode,opt) SET_BIT(mode,7,opt)
我正在用 C (ANSI C) 编写 POS(销售点)
我有这个功能
GetString(uchar *str,uchar mode,uchar minlen,uchar maxlen)
有点像 readln
但在 POS
在 API 中,mode
参数类似于 D1、D2、D3...
但在示例中(API)我有这个
if(!GetString(buf, 0x26, 0, 10))
{
buf[buf[0]+1]=0; amt=atol(buf+1);
} else {
/* user press CANCEL button */ }
那么0x26(函数中的参数mode
)和
二进制数或位标志,甚至,我不知道,十六进制。
在 API 中还有另一件事解释了 mode
输入参数
1. Input mode definition: (priority order is bit3>bit4>bit5, and mode&0x38≠0);
2. When inputting, the digit will be displayed on the screen in turns as plaintext or cryptograph (according to bit3).
3. The initial cursor position is determined by ScrGotoxy(x, y).
4. If bit7 of mode =1, then this function could bring initial digit string, and the string is displayed on initial cursor position as input digit string.
5. Output string does not record and contain function keys.
6. Press CLEAR button, if it is plaintext display, then CLEAR is considered as BACKSPACE key; if it is cryptograph display, then CLEAR is considered as the key to clear all contents.
7. Use function key to switch to Capital mode. S80 uses Alpha key to select the corresponding character on a key, however SP30 uses UP and Down key, and T52 uses ―#‖ key, T620 uses key F2.
8. In MT30, the switch method between uppercase, lowercase and number characters is to keep pressing
D1、D2、D3 ... D7 是位。我想它被用作一个位标志。因为它只有 1 个字节,所以它有 8 种可能的状态,所有这些状态都可以组合在一起。
0x26
是十进制38
还是二进制
00100110
这意味着 D1、D2、D5 已设置,其他所有 D 均未设置。
在 page you linked 中指定了 8 位标志。在您的示例中,您的十六进制值 0x26
是二进制值 00100110
。这指定了 8 个 bit-flags,其中 3 个(D1、D2、D5)已设置,5 个(D0、D3、D4、D6、D7)已清除。
如果你参考你的 table 链接(它是一个图形所以我不能粘贴它),它会告诉你 GetString
参数 mode
如何指示函数的行为, 对于 8 bit-flags 中的每一个设置 (1) 或清除 (0).
例如D2
设置为1表示左对齐.
组合各个标志得到一个二进制数,在您的示例中,它作为十六进制数传递 0x26
。
这对你有帮助,我定义了一个宏来操作 D7 位,根据文档是 ENTER 模式的位。
以相同的方式继续其他模式。
// bits manipulation
// idx stand for bit index
#define CHECK_BIT(var,idx) ((var >> idx) & 1)
#define SET_BIT(var,idx,n) (var ^= (-(n?1:0) ^ var) & (1 << idx))
// helping macros
// check if Enter mode is set D7 is used
#define IS_ENTER_MODE_SET(mode) CHECK_BIT(mode,7)
// set Enter mode to opt (true,false)
#define SET_ENTER_MODE (mode,opt) SET_BIT(mode,7,opt)