デジタルちんぶろぐ

デジタルな話題

ホーム 連絡をする 同期する ( RSS 2.0 ) Login
投稿数  268  : 記事  0  : コメント  4419  : トラックバック  79

ニュース


技術以外は
ちんぶろぐ

記事カテゴリ

書庫

日記カテゴリ

0x12345678
このデータをストアすると、各エンディアンで以下のようにメモリ上に保存されます。

Address Little Big
0000 78 12
0001 56 34
0002 34 56
0003 12 78

  x86のようなリトルエンディアン環境でビッグエンディアンで32bit値をストアしたい場合、僕がパっと思いつくのは以下の2つの方法です。

1. Byteサイズのポインタを使ってコピー
void store_be(DWORD data, void* pw)
{
    BYTE* pr = (BYTE*) &data;
    BYTE* pwb = (BYTE*) pw;
    pwb[0] = pr[3];
    pwb[1] = pr[2];
    pwb[2] = pr[1];
    pwb[3] = pr[0];
}

2. Big Endianへ変換して格納

#define CONVERT_ENDIAN(d) ((((d) << 24) & 0xff000000) | (((d) << 8) & 0x00ff0000) | (((d) >> 8) & 0x0000ff00) | (((d) >> 24) & 0x000000ff))
void store_be(DWORD data, void* pw)
{
    *((DWORD*) pw) = CONVERT_ENDIAN(data);
}

ここからが本題です。

問題は任意のビット長のデータを任意のビット位置から配置する場合です。
例えば0x123という有効ビット長が12bitのデータを各エンディアンで先頭バイトから詰まって
ゆくように格納する場合はどうなるでしょう?
メモリイメージで以下のように格納される場合です。

Address Little Big
0000 23 12
0001 *1 3*

※ ‘*’は未使用

このようにリトルエンディアンとビッグエンディアンでは単にバイトオーダーが違うだけでは
なくビット配置も異なります。
ビット走査(?)としては
・リトルエンディアンでは0->7(1byte目)8->15(2byte目)…
・ビッグエンディアンでは7->0(1byte目)15->8(2byte目)…
このことからわかるようにリトルエンディアンでは開始ビット位置+ビットサイズ-1で末尾ビット
位置を求めることができますが、ビッグエンディアンではそんなに単純にはできません。

このような場合、ビッグエンディアンのビット走査による位置の変化をリトルエンディアンと同じようにしてしまえば計算が楽になります。
つまり7->0->8->15のようなビット位置進行を0->7->8->15になるようにすれば良いのです。
これはビット位置を以下の変換式で変換してから計算することで可能です。
#define CONVERT_POS(n) (((n) | 7) - ((n) & 7))

上記の例でいえば7bit目(※ビッグエンディアンでは7bit目が最初のビットになる)から12bitの領域の末尾ですから、

CONVERT_POS(CONVERT_POS(7) + 12 ? 1);

これで末尾位置12を計算で求める事が出来ます。

このマクロと先のエンディアン変換マクロを併用することで任意位置から配置されるビッグエンディアンデータをマスクデータを含め比較的容易に作成することが出来ます。ただし、計算に用いている型のサイズを超えるデータを扱う場合や、境界を跨ぐ位置にデータが配置される場合は当然2回以上の計算が必要になりますが…

投稿日時 : 2009年6月24日 0:59

コメント

# re: [C] ビッグエンディアンとリトルエンディアンでのビット位置 2009/06/27 10:44 難箆 空那
void store_be(DWORD data, void* pw)
{
BYTE* pr = (BYTE*) &data;
BYTE* pwb = (BYTE*) pw;
pw[0] = pr[3];
pw[1] = pr[2];
pw[2] = pr[1];
pw[3] = pr[0];
}



void store_be(DWORD data, void* pw)
{
BYTE* pr = (BYTE*) &data;
BYTE* pwb = (BYTE*) pw;
pwb[0] = pr[3];
pwb[1] = pr[2];
pwb[2] = pr[1];
pwb[3] = pr[0];
}

?


# re: [C] ビッグエンディアンとリトルエンディアンでのビット位置 2009/06/27 14:55 あんどちん
ご指摘ありがとうございます。
その通りです。

# re: [C] ビッグエンディアンとリトルエンディアンでのビット位置 2009/06/27 14:58 あんどちん
難箆 空那さんのご指摘のとおり修正しました。


# A prvivcatooe insigh 2014/05/10 2:15 Dayana
A prvivcatooe insight! Just what we need!

# MggVwGPjKNAMdlinjub 2021/07/03 2:26 https://amzn.to/365xyVY
Network Advertising is naturally quite well-known because it can earn you a great deal of dollars within a pretty short period of time..

# Illikebuisse tarrb 2021/07/03 12:15 www.pharmaceptica.com
erectile herbal supplements https://www.pharmaceptica.com/

# Illikebuisse apulg 2021/07/05 0:09 pharmaceptica.com
hydroxychlor tab 200mg https://www.pharmaceptica.com/

# erectile pills in canada 2021/07/07 12:44 hydroxychloroquine sulfate 200
hydroxychloroqine https://plaquenilx.com/# hydroxychloroquine use

# re: [C] ?????????????????????????? 2021/07/26 5:38 hydroxychloroquine 200 mg tablets
chloroquinone https://chloroquineorigin.com/# hydroxyl chloroquine

# nsuqizzhnvte 2021/11/25 8:48 dwedayvkab
plaquenil coupon https://aralenquinesab.com/

# ralgedxbhmbf 2021/12/04 0:02 dwedaycgev
https://chloroquinesbtc.com/

# ivermectin buy nz http://stromectolabc.com/
stromectol tab 3mg 2022/02/08 2:44 Busjdhj
ivermectin buy nz http://stromectolabc.com/
stromectol tab 3mg

# ivermectin 2ml http://stromectolabc.com/
stromectol tab 3mg 2022/02/08 9:31 Busjdhj
ivermectin 2ml http://stromectolabc.com/
stromectol tab 3mg

# buy ivermectin uk http://stromectolabc.com/
stromectol cream 2022/02/08 16:35 Busjdhj
buy ivermectin uk http://stromectolabc.com/
stromectol cream

# doxycycline online https://doxycyline1st.com/
doxycycline hyc 2022/02/26 8:19 Doxycycline
doxycycline online https://doxycyline1st.com/
doxycycline hyc

# order doxycycline online https://doxycyline1st.com/
purchase doxycycline online 2022/02/26 20:00 Doxycycline
order doxycycline online https://doxycyline1st.com/
purchase doxycycline online

# jubcljvnzhpw 2022/05/23 5:34 sunmchwp
http://erythromycin1m.com/# erythromycin ophth ointment 3.5 gm

# ed pills for sale https://erectionpills.best/
men's ed pills 2022/06/28 19:48 ErectionPills
ed pills for sale https://erectionpills.best/
men's ed pills

# paxlovid contraindications https://paxlovid.best/
antivirals for covid treatment 2022/09/08 7:33 Paxlovid
paxlovid contraindications https://paxlovid.best/
antivirals for covid treatment

# online order prednisone 10mg https://prednisone20mg.site/
non prescription prednisone 20mg 2022/11/15 17:54 Prednisone
online order prednisone 10mg https://prednisone20mg.site/
non prescription prednisone 20mg

# free dating sites free https://datingsiteonline.site/
personal ads free 2022/12/05 23:45 Tading
free dating sites free https://datingsiteonline.site/
personal ads free

# generic aralen 250 mg 2022/12/27 3:03 MorrisReaks
hydroxychloroquine online cheap dr collins https://hydroxychloroquinex.com/

# aralen price usa 2022/12/29 23:08 MorrisReaks
http://hydroxychloroquinex.com/ chloroquine phosphate tablets

# ed drug prices https://edpills.science/
ed pills cheap 2023/01/07 13:47 EdPills
ed drug prices https://edpills.science/
ed pills cheap

# Read information now. Cautions.
https://edonlinefast.com
Get information now. Learn about the side effects, dosages, and interactions. 2023/02/17 7:16 EdOnline
Read information now. Cautions.
https://edonlinefast.com
Get information now. Learn about the side effects, dosages, and interactions.

# doors2.txt;1 2023/03/14 14:45 CUJBDzVonADUtdXVgCg
doors2.txt;1

# doors2.txt;1 2023/03/14 16:21 eTCZdYfguQwNFPV
doors2.txt;1

# canadian drug store online https://pillswithoutprescription.pro/# 2023/05/14 22:10 PillsPresc
canadian drug store online https://pillswithoutprescription.pro/#

# professional dating sites 2023/08/09 13:58 WayneGurry
good free dating wesbites: https://datingtopreview.com/# - christian dating for free

# order cytotec online 2023/08/28 20:21 Georgejep
http://avodart.pro/# how to buy avodart price

# п»їfarmacia online migliore 2023/09/26 6:57 Archieonelf
https://farmaciaonline.men/# farmacie online sicure

# versandapotheke 2023/09/26 13:22 Williamreomo
http://onlineapotheke.tech/# versandapotheke
online apotheke gГ?nstig

# internet apotheke 2023/09/26 15:22 Williamreomo
https://onlineapotheke.tech/# versandapotheke deutschland
versandapotheke

# online apotheke preisvergleich 2023/09/26 23:09 Williamreomo
http://onlineapotheke.tech/# online apotheke gГ?nstig
versandapotheke

# online apotheke gГјnstig 2023/09/27 0:33 Williamreomo
https://onlineapotheke.tech/# online apotheke versandkostenfrei
versandapotheke

# online apotheke deutschland 2023/09/27 1:29 Williamreomo
http://onlineapotheke.tech/# gГ?nstige online apotheke
versandapotheke deutschland

# gГјnstige online apotheke 2023/09/27 2:02 Williamreomo
https://onlineapotheke.tech/# versandapotheke deutschland
versandapotheke deutschland

# п»їonline apotheke 2023/09/27 3:53 Williamreomo
http://onlineapotheke.tech/# versandapotheke
gГ?nstige online apotheke

# п»їonline apotheke 2023/09/27 12:51 Williamreomo
https://onlineapotheke.tech/# internet apotheke
п»?online apotheke

# farmaci senza ricetta elenco 2023/09/27 17:13 Rickeyrof
acheter sildenafil 100mg sans ordonnance

# п»їonline apotheke 2023/09/27 19:53 Terrywef
acheter sildenafil 100mg sans ordonnance

# farmacie on line spedizione gratuita 2023/09/27 20:59 Rickeyrof
acheter sildenafil 100mg sans ordonnance

# farmacie online autorizzate elenco 2023/09/27 21:15 Rickeyrof
acheter sildenafil 100mg sans ordonnance

# canada drug stores 2023/10/17 1:51 Dannyhealm
Their 24/7 support line is super helpful. http://mexicanpharmonline.com/# mexican border pharmacies shipping to usa

# fst dispensary 2023/10/17 2:54 Dannyhealm
Always on the pulse of international healthcare developments. http://mexicanpharmonline.shop/# mexican border pharmacies shipping to usa

# canadian oharmacy 2023/10/17 15:13 Dannyhealm
Always a seamless experience, whether ordering domestically or internationally. https://mexicanpharmonline.com/# mexican pharmaceuticals online

# pharmacies in canada online 2023/10/18 3:40 Dannyhealm
Speedy service with a smile! http://mexicanpharmonline.com/# mexican mail order pharmacies

# canadian mail order pharmacies 2023/10/18 6:30 Dannyhealm
They're reshaping international pharmaceutical care. http://mexicanpharmonline.com/# mexico drug stores pharmacies

# canadian pharamacy 2023/10/18 7:41 Dannyhealm
Best and news about drug. http://mexicanpharmonline.com/# mexican border pharmacies shipping to usa

# how to get a prescription in canada 2023/10/19 7:19 Dannyhealm
Learn about the side effects, dosages, and interactions. http://mexicanpharmonline.com/# mexican pharmaceuticals online

# valtrex 500mg best price https://valtrex.auction/ valtrex rx 2023/10/24 17:43 Valtrex
valtrex 500mg best price https://valtrex.auction/ valtrex rx

# п»їpaxlovid 2023/10/25 0:15 LarryNef
https://plavix.guru/# plavix best price

# cheapest ed pills online 2023/11/20 17:06 WilliamApomb
https://sildenafil.win/# sildenafil online singapore

# buy ed pills online 2023/11/23 13:44 WilliamApomb
https://sildenafil.win/# sildenafil brand name in canada

# doxy 200 https://doxycycline.forum/ order doxycycline 2023/11/25 9:07 Doxycycline
doxy 200 https://doxycycline.forum/ order doxycycline

# no 1 canadian pharcharmy online 2023/11/29 19:07 MichaelBum
https://claritin.icu/# ventolin online nz

# Sildenafil Preis 2023/12/17 17:33 StevenNuant
http://apotheke.company/# online apotheke deutschland

# mens ed pills https://edpills.tech/# cheapest ed pills 2023/12/23 4:40 EdPills
mens ed pills https://edpills.tech/# cheapest ed pills

# joint pain peripheral neuropathy eos level severe tingling 2024/06/02 1:09 hierneEret
http://jointpain.top/ - foot pain side of big toe joint

Post Feedback

タイトル
名前
Url:
コメント: