[英]How does bit flag parameters of a method works (Ansi C/C)?
I'm programming a POS (point of sale) in C (ANSI C) 我正在用C(ANSI C)编程POS(销售点)
I have this function 我有这个功能
GetString(uchar *str,uchar mode,uchar minlen,uchar maxlen)
Is something like readln
but in the POS 是类似于
readln
但在POS中
In the API the mode
parameter is something like D1,D2,D3... 在API中 ,
mode
参数类似于D1,D2,D3 ...
But in the example (of the API) i have this 但是在(API的)示例中,我有这个
if(!GetString(buf, 0x26, 0, 10))
{
buf[buf[0]+1]=0; amt=atol(buf+1);
} else {
/* user press CANCEL button */ }
So what is the relation betwen 0x26 (parameter mode
in the function) and the binary numbers or bit flag or even, I dont know, hexadecimal. 那么0x26 (函数中的参数
mode
)与二进制数或位标志甚至十六进制之间的关系是什么。
In the API theres another thing explaining the mode
input parameter 在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
The D1, D2, D3 ... D7 are bits. D1,D2,D3 ... D7是位。 I suppose it's used as a bit flag.
我想它被用作位标志。 Since it's just 1 byte it has 8 possible states all of them can be combined together.
由于它只有1个字节,因此具有8种可能的状态,所有这些状态都可以组合在一起。
The 0x26
is decimal 38
or binary 0x26
是十进制38
或二进制
00100110
which means D1, D2, D5 are set and al the other D's are not. 这意味着D1,D2,D5已设置,而其他D均未设置。
In the page you linked there are 8 bit flags specified. 在您链接的页面中,指定了8位标志。 In your example you have the hex value
0x26
which is the binary value 00100110
. 在您的示例中,您具有十六进制值
0x26
,它是二进制值00100110
。 This specifies 8 bit-flags, of which 3 (D1, D2, D5) are set and 5 (D0, D3, D4, D6, D7) are clear. 这指定了8位标志,其中设置了3个(D1,D2,D5),清除了5个(D0,D3,D4,D6,D7)。
If you refer to your table linked (it's a graphic so I can't paste it), it tells you how the GetString
argument mode
instructs the function to behave, for each of the 8 bit-flags set (1) or clear (0). 如果您引用链接的表(这是一个图形,所以我无法粘贴它),它会告诉您
GetString
参数mode
如何指示函数对设置(1)或清除(0)的8位标志中的每一个)。
For example D2
set to 1 indicates left alignment . 例如,将
D2
设置为1表示左对齐 。
Combining the individual flags gives a binary number, which in your example is passed as the hexadecimal number 0x26
. 组合各个标志给出一个二进制数,在您的示例中将其作为十六进制数
0x26
。
this will helps you , i defined a macro to manipulate D7 bit which is according to documentation is the bit of the ENTER mode. 这将对您有帮助,我定义了一个宏来操纵D7位,根据文档所述,该位是ENTER模式的位。
continue in the same way with the others modes. 以其他方式继续操作。
// 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.