2011-04-25 ラップアラウンドの処理はめんどう
■[開発]ラップアラウンドの処理はめんどう
32ビット値でインデックスとオフセットを受け取って、対象のアドレスになんかの操作をするような関数があったとする。
とりあえず、インデックスは最大100までとすると、範囲が超えてないかチェックする必要がでてくるわけで。
bool test(unsigned int index, unsigned int offset) { //範囲チェック if(100 < index + offset) { //エラー return false; } //何かの処理 return true; }
これだと、index + offsetが32ビット値を超えるときに範囲内に戻っちゃうのでエラーにならない。
じゃぁ、一回64ビット値に入れればいいじゃないかと。
bool test(unsigned int index, unsigned int offset) { //範囲チェック long long targetIndex = index + offset; if(100 < targetIndex) { //エラー return false; } //何かの処理 return true; }
これ、ダメなんですよね。
せっかく64ビット変数を用意したのに、そこに入るのは32ビットの計算結果。
なのですでにラップアラウンドした後。
bool test(unsigned int index, unsigned int offset) { //範囲チェック long long targetIndex = index; targetIndex += offset; if(100 < targetIndex) { //エラー return false; } //何かの処理 return true; }
いったん64ビット値にしたあと、加算すればOK・・・だけどなんか汚いなぁ・・・。
トラックバック - http://d.hatena.ne.jp/cjohn/20110425/1303735176
リンク元
- 5 http://www.google.co.jp/search?q=ラップアラウンド処理&hl=ja&rls=com.microsoft:en-US&sa=2
- 4 http://www.google.co.jp/url?sa=t&rct=j&q=ラップアラウンド処理&source=web&cd=4&ved=0CEIQFjAD&url=http://d.hatena.ne.jp/cjohn/20110425/1303735176&ei=R1siT_DCBaTAiQe3r6zoBA&usg
- 3 http://twitter.com/
- 2 http://ezsch.ezweb.ne.jp/search/?sr=0101&query=DIVE!! キャスト
- 2 http://www.google.co.jp/url?sa=t&rct=j&q=アドレスの演算 ラップアラウンド&source=web&cd=4&ved=0CEcQFjAD&url=http://d.hatena.ne.jp/cjohn
- 2 http://www.google.co.jp/url?sa=t&source=web&cd=2&ved=0CCAQFjAB&url=http://d.hatena.ne.jp/cjohn/?of=0&rct=j&q=.TargetIndex javascript&ei=WdHETb_kMIK2vQOBy5WRAQ&usg=AFQjCNFSEM9ONYlBG4KFJoViOArM0avAZQ
- 1 http://cpp.ring.hatena.ne.jp/
- 1 http://d.hatena.ne.jp/authorNari/20110501/1304233409?utm_source=twitterfeed&utm_medium=twitter
- 1 http://pipes.yahoo.com/pipes/pipe.info?_id=VPw6npu13RGKo15vBRNMsA
- 1 http://reader.livedoor.com/reader/