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)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 蚌埠市| 华池县| 四会市| 宜章县| 武义县| 赤水市| 临西县| 应用必备| 临沭县| 宝清县| 孝感市| 弋阳县| 长治市| 龙陵县| 巴林左旗| 革吉县| 安化县| 油尖旺区| 长沙市| 峨山| 仙居县| 贵州省| 连江县| 固原市| 临泉县| 巫溪县| 枣强县| 牙克石市| 青州市| 巨野县| 隆子县| 曲阳县| 新平| 秦皇岛市| 扎赉特旗| 武安市| 北川| 筠连县| 炎陵县| 伊金霍洛旗| 东海县|