簡體   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