Oo緣來是你oO


          posts - 120,comments - 125,trackbacks - 0

                                              打印整型的二進(jìn)制格式
                                              
                                                          
          馬嘉楠
               2008.3.24


          一共2個(gè)文件:
          PrintBinary.java       ---
          打印整形的二進(jìn)制格式
          PrintBinaryTest.java ---
          測試文件


          主要內(nèi)容:
          打印整型的二進(jìn)制格式

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

          文件1:PrintBinary.java

          package Common.Utils.DataUtils;

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

          public class PrintBinary {
              
          /**
               * 打印整型的二進(jìn)制格式
               * 
               * 
          @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二進(jìn)制形式(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 馬嘉楠 閱讀(1218) 評論(0)  編輯  收藏

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 青阳县| 昆明市| 星座| 陆川县| 龙山县| 满城县| 云阳县| 连州市| 柯坪县| 盐亭县| 富宁县| 新昌县| 凤冈县| 玉屏| 肃北| 胶南市| 体育| 伽师县| 囊谦县| 灌阳县| 宝兴县| 临高县| 大渡口区| 黑河市| 威信县| 偃师市| 满洲里市| 阳西县| 赤峰市| 建始县| 铁岭县| 晴隆县| 江津市| 松江区| 岑巩县| 武邑县| 合水县| 伊宁市| 大洼县| 丹阳市| 江门市|