Oo緣來是你oO


          posts - 120,comments - 125,trackbacks - 0

                                              打印整型的二進制格式
                                              
                                                          
          馬嘉楠
               2008.3.24


          一共2個文件:
          PrintBinary.java       ---
          打印整形的二進制格式
          PrintBinaryTest.java ---
          測試文件


          主要內容:
          打印整型的二進制格式

          主要功能:
          1.   void printBinaryNumber(int x) 
                
          打印整型的二進制格式

          文件1:PrintBinary.java

          package Common.Utils.DataUtils;

          /**
           * 
           * <p>
           * Description: 打印整型的二進制格式
           * </p>
           * <p>
           * Copyright 2006 mjn.
           * </p>
           * 
           * 
          @author ma jia nan
           * @Create Date : 2008-02-24
           * @Version : 1.0
           
          */

          public class PrintBinary {
              
          /**
               * 打印整型的二進制格式
               * 
               * 
          @param x
               * 
          @return

               
          */

              
          public void printBinaryNumber(int x) {
                  
          int[] buffer = new int[32
          ];
                  
          for (int i = 0, mask = 1; i < 32; i++
          {
                      mask 
          = 1
          ;
                      mask 
          = mask <<
           i;
                      buffer[i] 
          = (mask & x) >>
           i;
                  }

                  
          for (int j = 31; j >= 0; j--{
                      System.out.print(buffer[j]);
                      
          if (j % 8 == 0
          )
                          System.out.print(
          " "
          );
                  }

                  System.out.println(
          "");
              }

          }



          文件2:PrintBinaryTest.java

          import Common.Utils.DataUtils.PrintBinary;

          /**
           * 
           * <p>
           * Description: 測試PrintBinary
           * </p>
           * <p>
           * Copyright 2006 mjn.
           * </p>
           * 
           * 
          @author ma jia nan
           * @Create Date : 2008-03-24
           * @Version : 1.0
           
          */


          public class PrintBinaryTest {

              
          public static void main(String args[]) 
          {
                  System.out.println(s((
          byte100
          ));
                  System.out.println(s((
          byte1
          ));
                  System.out.println(s((
          byte-1
          ));

              }


              
          private static byte s(byte b) {
                  
          return new
           test().swap(b);
              }


              
          private void p(int x) {
                  
          new
           PrintBinary().printBinaryNumber(x);
              }


              
          /**
               * 交換b二進制形式(8位)的前4位和后4位
               * 
               * 
          @param b
               * 
          @return

               
          */

              
          public byte swap(byte b) {
                  
          int lowBits = b & 0xF
          ;
                  
          int highBits = b & 0xF0
          ;
                  
          int result = lowBits << 4 | highBits >> 4
          ;

                  System.out.println(
          "\n\nb=" + b + "  0xF=" + 0xF + "  0xF0=" + 0xF0
          );
                  System.out.print(
          "      b  Binary : "
          );
                  p(b);
                  System.out.print(
          "    0xF  Binary : "
          );
                  p(
          0xF
          );
                  System.out.print(
          " lowBits(b&0xF) : "
          );
                  p((b 
          & 0xF
          ));

                  System.out.print(
          "\n      b  Binary : "
          );
                  p(b);
                  System.out.print(
          "   0xF0  Binary : "
          );
                  p(
          0xF0
          );
                  System.out.print(
          "highBits(b&0xF0): "
          );
                  p((b 
          & 0xF0
          ));

                  System.out.print(
          "\n   lowBits << 4 : "
          );
                  p((lowBits 
          << 4
          ));
                  System.out.print(
          "  highBits >> 4 : "
          );
                  p((highBits 
          >>> 4
          ));
                  System.out.print(
          "    result( | ) : "
          );
                  p((result));

                  
          return (byte
          ) result;
              }


          }





          馬嘉楠
          jianan.ma@gmail.com

          posted on 2008-03-24 10:51 馬嘉楠 閱讀(1209) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 红原县| 湘乡市| 财经| 普洱| 泗洪县| 莎车县| 兴化市| 永福县| 白玉县| 临泽县| 汉中市| 盐池县| 芷江| 内乡县| 灵璧县| 灵丘县| 板桥市| 华坪县| 顺昌县| 云林县| 石阡县| 清水河县| 维西| 沁阳市| 融水| 吉安县| 乌鲁木齐县| 江口县| 克什克腾旗| 古浪县| 瑞安市| 炎陵县| 新和县| 区。| 岳池县| 益阳市| 湘潭市| 新余市| 灵璧县| 沧源| 琼结县|