简体   繁体   中英

How does bit flag parameters of a method works (Ansi C/C)?

I'm programming a POS (point of sale) in C (ANSI C)

I have this function

GetString(uchar *str,uchar mode,uchar minlen,uchar maxlen)

Is something like readln but in the POS

In the API the mode parameter is something like D1,D2,D3...

But in the example (of the API) i have this

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.

In the API theres another thing explaining the mode input parameter

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. 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.

The 0x26 is decimal 38 or binary

00100110

which means D1, D2, D5 are set and al the other D's are not.

In the page you linked there are 8 bit flags specified. In your example you have the hex value 0x26 which is the binary value 00100110 . This specifies 8 bit-flags, of which 3 (D1, D2, D5) are set and 5 (D0, D3, D4, D6, D7) are clear.

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).

For example D2 set to 1 indicates left alignment .

Combining the individual flags gives a binary number, which in your example is passed as the hexadecimal number 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.

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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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