繁体   English   中英

方法的位标志参数如何工作(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均未设置。

您链接页面中,指定了8位标志。 在您的示例中,您具有十六进制值0x26 ,它是二进制值00100110 这指定了8位标志,其中设置了3个(D1,D2,D5),清除了5个(D0,D3,D4,D6,D7)。

如果您引用链接的表(这是一个图形,所以我无法粘贴它),它会告诉您GetString参数mode如何指示函数对设置(1)或清除(0)的8位标志中的每一个)。

例如,将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) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM