C/C++の宿題を片付けます 117代目at TECH
C/C++の宿題を片付けます 117代目 - 暇つぶし2ch114:デフォルトの名無しさん
08/11/03 12:40:22
>>112
#include <stdio.h>
#include <limits.h>
int is_little_endian(){
  int x = 1;
  return *(char*)&x;
}
typedef unsigned char byte_t;
void print_bytebits_(byte_t value){
  byte_t mask = 1 << (sizeof(byte_t) * CHAR_BIT - 1);
  for(;mask; mask >>=1) putchar(mask & value ? '1' : '0');
}
void print_bits(void *p, int bytesize){
  byte_t *p_byte = (byte_t*)p;
  int i;
  if(is_little_endian()){
    for(i=bytesize-1; i>=0; i--) print_bytebits_(p_byte[i]);
  }else{
    for(i=0; i<bytesize; i++) print_bytebits_(p_byte[i]);
  }
}
int main(){
  double values[] = {1.0, 0.1, 0.0/0.0, -1.0/0.0, 1.0/0.0, -3.14};
  int i;
  for(i=0; i<sizeof(values)/sizeof(*values); i++){
    print_bits(&values[i], sizeof(double));
    printf("(%.02f)\n", values[i]);
  }
  return 0;
}


次ページ
続きを表示
1を表示
最新レス表示
レスジャンプ
類似スレ一覧
スレッドの検索
話題のニュース
おまかせリスト
オプション
しおりを挟む
スレッドに書込
スレッドの一覧
暇つぶし2ch