2017年5月20日 星期六

C and C++ 如何用簡易的方式提取 特定位元(bit) 又可直接存取整個數據

C and C++ 如何用簡易的方式提取 特定位元(bit) 又可直接存取整個數據

這裡用了幾個技巧來達成這個功能
  • Union
  • struct 位元切割
  • 位元運算

Union

用一句話形容
可以自由的切換檢視角度
這是我自己創的詞可能不是很正確,不過可以很容易的理解
在記憶體內全部都是0和1,並不會區別什麼是 int 什麼是 char 那只不過是經過編譯器的解析,或的正確的數值。
檢視角度指的是對記憶體資料的型態解析,比如說以下的例子
int i=65;
void* p=&i;
現在 P 指向某個記憶體位置,但是不知道他是什麼型態
現在詳細指明他是什麼型態 (先不管成功與否的安全問題)
char* str = (char*)p;
現在你告訴編譯器,當我找str地址的使用char的型態來檢視
cout << str << endl;
現在你可以正確的顯示 ‘A’
這個過程需要指名轉型,使用Union可以共用記憶體
共用之後需要什麼型態就直接用什麼型態
自由的切換檢視角度
union S{
    int i;
    char c;
};
現在我們讓 int 與 char 共用一塊記憶體
    S a;
    a.i = 65;
    cout << "a.c = " << a.c << endl;
結果顯示為 A,可以省去繁瑣的轉型,需要什麼在補上什麼
另外~浮點數類的如 float doubel 算法不一樣直接轉數值會不是你想要的,不能這樣用


Struct 位元切割

Struct與Union配合可以任意的取出任意位元
using uch = unsigned char;
union Bit{
    struct Bit_dev{
        uch data: 4;
        uch: 2;
        uch data2: 2;
    } bit;
    uch raw;
};
};
這裡特別的寫法可以獲取特定的位元,不要的部分可以不用設變數
養成好習慣記得把struct的大小分配補滿,比較不會產生誤會
如果你只要取中間的話

using uch = unsigned char;
union Bit{
    struct Bit_dev{
        uch:3;
        uch data:4;
        uch:1; // 可以省略不過補上比較好
    } bit;
    uch raw;
};
用法如下
/*****************************************************************
Name :
Date : 2017/05/07
By   : CharlotteHonG
Final: 2017/05/07
*****************************************************************/
#include <iostream>
#include <bitset>

using uch = unsigned char;
union Bit{
    struct Bit_dev{
        uch data: 4;
        uch: 2;
        uch data2: 2;
    } bit;
    uch raw;
};

int main(int argc, char const *argv[]) {
    S a;
    a.i = 65;
    cout << "a.c = " << a.c << endl;

    Bit b;
    b.raw = 0xF7;
    cout << "b.bit.data=" << (bitset<8>)b.raw << endl;
    cout << "b.bit.data=" << (int)b.bit.data << endl;
    cout << "b.bit.data=" << (int)b.bit.data2 << endl;

    return 0;
}
結果
a.c = A
b.bit.data=11110111
b.bit.data=7
b.bit.data=3
需要注意的部分是順序從左開始還是從右開始,自己看一下很容易可以看出


位元運算

這個比較沒有難度,不過可能不好理解,邏輯運算 &
unsigned char bit = 0xCF;
cout << "bit=" << (bitset<8>)bit << endl;
會印出 11001111 目標是取出中間4個 0011 也就是 3
先右移兩位把不要的推掉
bit >>= 2;
cout << "bit=" << (bitset<8>)bit << endl; // bit=00110011
然後讓右邊4個做 & 運算 (左邊的沒寫到的會被捨去)
bit &= 0xF; // 1111
cout << "bit=" << (bitset<8>)bit << endl; // bit=00000011
如果要取3個的話就不要最左邊的
bit &= 0x7; // 0111

android sony 手機的 skype 無法使用表情圖案,出現鍵盤

android sony 手機的 skype 無法使用表情圖案,出現鍵盤

很久之前遇過一次最近又出現了一次,症狀就是skype怎麼樣都無法按出表情,按下表情欄位會變成打字鍵盤。
主要是某些可以重製繪圖的背景軟體造成的,之前是因為裝了 easy tough ,手機桌面會有一顆iphone浮球,導致出錯。
後來將那個軟體移除即可
最近的一次的是因為 ES File 檔案管理員更新了!新增了一個浮球的功能導致出問題的,不過我已經有嘗試去軟體設定內關閉浮球還是一樣。
貌似有兩個浮球,一個是縮小到桌面出現的,一個是ES File軟體內的,兩個都要關閉的樣子,我那時候只有關一個。
解決辦法我是重新裝,然後第一次開啟的引導介面可以選擇不要開,直接在那裏選擇不開。
後來推測或許兩個都關掉應該就不用重裝了。

2017年5月5日 星期五

如何降低編譯的依存關係

如何降低編譯的依存關係

tags: C++ Concept2

依存關係

依存關係指的是A class的實作必須在寫在B class之前,他們之間存在相依關係,A一定要寫在B之前;或A檔案一定要先被編譯,他們之間不能夠單純依靠宣告來通過編譯。
以下降低解說難易度將檔案縮減至一個單一檔案敘述
假如有一個自訂類別為 Name
// Name class 前置性宣告
class Name;
// Name class 定義 (不是成員函式定義)
class Name{
public:
    Name(string n);
    friend ostream& operator<<(ostream& out, Name const & n);
private:
    string n;
};
那麼要使用這個型別的時候,該class定義必須出現在使用之前
// Name class 宣告
// Name class 定義
Name a("chg");
如果你把class定義移動到下方則會出問題
(Name的函式定義可以移動到下方)
// Name class 宣告
Name a("chg");
// Name class 定義
則會出現相依的問題,產生Name的時候找不到定義;其他class如果要使用Name這個class,會造成Name一定要擺在那之上才可以編譯,如果要交錯互相使用麻煩就大了,誰在上都不對。


Handle Class

解決辦法,可以使用指針來占個位置不把它實作,這樣就不會需要他的實作了
只需要前置性宣告。
// Name class 宣告
Name* a;
// Name class 定義
應用在剛剛說的問題上代碼大概就長這個樣子
// Tw class定義
class Name;
class Tw{
public:
    Tw(string name);
private:
    Name* n;
};
// Name class定義
class Name{
public:
    Name(string n);
    friend ostream& operator<<(ostream& out, Name const & n);
private:
    string n;
};

// Tw建構子
Tw::Tw(string name): n(new Name(name)){
    cout << "n=" << *n << endl;
}
// Name 函式定義
Name::Name(string n): n(n){}
ostream& operator<<(ostream& out, Name const & n){
    out << n.n;
    return out;
}
如此一來順序的相依性就變得兩者皆可(函式的定義也是)
此外這裡的指針也可以使用沒有初始化的vector取代
vector<Name> n;
不要初始化內容
vector<Name> n("name");
也不要初始化長度
vector<Name> n(1);
反例中的代碼因為Name沒有預設建構子(引數可為無的建構子),僅能使用 push_back() 初始化
// Tw建構子
Tw::Tw(string name){
    n.push_back(name);
    cout << "n=" << n[0] << endl;
}


Protocol Class

利用虛擬函式的特性,不需要先做,藉此避開宣告時必須要要定義的狀況。
// Name class 宣告
// 純虛擬類別定義(抽象類別宣告內含Name)

// Name class 定義
// 繼承抽象類別


參考代碼

Handle Class
/*****************************************************************
Name : Handle Class
Date : 2017/05/04
By   : CharlotteHonG
Final: 2017/05/04
*****************************************************************/
#include <iostream>
#include <string>
#include <vector>
using namespace std;

// Tw class定義
class Name;
class Tw{
public:
    Tw(string name);
private:
    Name* n;
    // vector<Name> n;
};

//================================================================
int main(int argc, char const *argv[]){
    Tw a("chg");
    return 0;
}
//================================================================

// Name class定義
class Name{
public:
    Name(string n);
    friend ostream& operator<<(ostream& out, Name const & n);
private:
    string n;
};
// Tw建構子
Tw::Tw(string name){
    n = new Name(name);   // point
    // n.push_back(name); // vector
    cout << "n=" << n[0] << endl;
}
// Name 函式定義
Name::Name(string n): n(n){}
ostream& operator<<(ostream& out, Name const & n){
    out << n.n;
    return out;
}
Protocol Class
/*****************************************************************
Name :
Date : 2017/05/04
By   : CharlotteHonG
Final: 2017/05/05
*****************************************************************/
#include <iostream>
using namespace std;
// Tw class定義
class Name;
class Tw{
public:
    virtual ~Tw(){}
    virtual string name()=0;
    static Tw* makeTw(Name const & name);
};

// Name class定義
class Name{
public:
    Name(string const & n);
    operator string&(){
        return n;
    }
    friend ostream& operator<<(ostream& out, Name const & n);
private:
    string n;
};
//================================================================
int main(int argc, char const *argv[]){
    Tw* p;
    Name chg = string("chg");
    p = Tw::makeTw(chg);
    cout << p->name() << endl;
    return 0;
}
//================================================================

// Tw class定義
class RealTw: public Tw{
public:
    RealTw(Name const & name): n(name){}
    string name(){return n;}
private:
    Name n;
};

// makeTw 函式定義
Tw* Tw::makeTw(Name const & name){
    return new RealTw(name);
}

// Name 函式定義
Name::Name(string const & n): n(n){}
ostream& operator<<(ostream& out, Name const & n){
    out << n.n;
    return out;
}

operator 運算子的各式標準寫法與原因

operator 運算子的各式標準寫法與原因

tags: C++ Concept2
operator存在著許多應該遵守的公約,這裡會列出常見的operator如何撰寫標準公約,並說明為何要如此撰寫與舉例


宣告

/* 那些只有一行的函式可以直接上inline */

// +運算子
Arr & operator+=(Arr const &rhs);
friend Arr const operator+(Arr const &lhs, Arr const &rhs);
    return Arr(lhs) += rhs; // op+的定義

// 下標運算子
int & operator[](size_t idx){
    return const_cast<int&>(static_cast<const Arr&>(*this)[idx]);
}
const int & operator[](size_t idx) const;

// <<運算符號
friend ostream & operator<< (ostream& s, const Rati & r);

// 複製建構子
List(List const & rhs);
// 複製函式
List & operator=(List const & rhs);

// 移動建構子
List(List && rhs){
    (*this) = std::move(rhs);
}
// 移動函式
List & operator=(List && rhs);

// 取址
T* operator&();
// 取值(轉型)
operator T&();

// ++T
Arr & operator++();
// T++
Arr operator++(int);


公約

盡可能記住他,在大多數的可以幫你避開很多坑

參數

看一下以下的例子,這樣的寫法可以同時接收const與non-const
void fun(int const & num){
    cout << "num=" << num << endl;
}

int i=0;
fun(i);
  1. 避免寫兩個 fun() 函式
  2. 不小心修改到時編譯器會提醒你
參數能夠加上 const 就加上 const

返回的物件

假設一個沒有被加上const物件被返回可能會發生這種事情
Arr operator+(Arr const &lhs, Arr const &rhs);

Arr a, b;
a+a=b;
或許你不會這寫,但不能保證沒有人會這樣寫,嘗試對一個即將被解構的物件修改,其結果只是浪費效能。
返回的物件能夠加上 const 就加上 const

返回 (*this) 的參考

我們應該效仿基本的型別操作,int這些基礎型別是可以連續被指定的
int a, b, c, d;
a=b=c=d=1;
我們只要傳出 (*this) 的參考就可以做到這樣的操作,可以把他們拆解成原本的樣態,可以更容易理解
Arr a, b, c;
a=b=c;

a.operator=(b.operator=(c));

適當的使用全域函式

當我們使用 op+() 的時候要注意一種情況
Arr a;
a+1;
這把它還原看起來沒問題
a.operator+(1);
可是反過來就出問題
1.operator+(a);
這可以使用全域函式處理
Arr const operator+(int lhs, Arr const &rhs);
Arr const operator+(Arr const &lhs, int rhs);

適當的引用重複的函式

可以觀察到某些函式其實是具有重複性的
  • op+() 可以拆解為兩個已有的函式,複製後+=
  • 下標負號的 const 與 non-const
  • 移動建構子的內容與移動函式相等
  • 複製建構子與複製函式部分相同(額外拆一個函式呼叫)

拆解

一個相加的函式可以拆解為
int a=1, b=2;

int temp=a;
temp+=b;
下標符號比較特別需要特別為了 const 與 non-const 寫兩個完全一樣的函式
int & operator[](size_t idx);
const int & operator[](size_t idx) const;

Arr a;
const Arr b;

a[0]=1;
b[0];
解決方法長這個樣子
int & operator[](size_t idx){
    return const_cast<int&>(static_cast<const Arr&>(*this)[idx]);
}
簡單來說從右邊看回來
  1. 先加上const屬性
  2. 然後讓它去呼叫const的op[]()函式
  3. 再強制解除他的 const 屬性
順序不要反過來了把主代碼寫在 non-const (是可以正常運行的)
讓 const 去呼叫 non-const 的函式
這樣會導致函式運行的時候 const 屬性被解除可以被修改。
難保後面的人沒察覺到…(通常這個人是幾個月後的自己)

補上 friend 開放 private 的存取權限

某些函式寫在全域函式會比寫在成員函式還要來的好,可是一旦寫到全域函式去之後,相對的就會失去對private成員的存取權限
在類別的定義裡面補上該全域函式的 friend宣告,讓全域函式可以有應有的權限操作成員函式

待續