Convert PadLeft Bit Operate

Posted sky20080101

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Convert PadLeft Bit Operate相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleAppConvert
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i < 10;i++ )
            {
                String str = Convert.ToString(i).PadLeft(8, 0);
                Console.WriteLine(str);
            }
            Console.WriteLine("=================================");
            byte b = 0xC1;
            if ((b>>7)!=0)
            {
                Console.WriteLine("{0} ‘s bit 8 is 1", Convert.ToString(b, 2).PadLeft(8, 0));
            } 
            else
            {
                Console.WriteLine("{0} ‘s bit 8 is 0", Convert.ToString(b, 2).PadLeft(8, 0));
            }

            b = 0x41;
            if ((b >> 7) != 0)
            {
                Console.WriteLine("{0} ‘s bit 8 is 1", Convert.ToString(b, 2).PadLeft(8, 0));
            }
            else
            {
                Console.WriteLine("{0} ‘s bit 8 is 0", Convert.ToString(b, 2).PadLeft(8, 0));
            }
/*
11000001 ‘s bit 8 is 1
01000001 ‘s bit 8 is 0           
*/
            ///////////////////////////////////////
        }
    }
}
/*
00000001
00000002
00000003
00000004
00000005
00000006
00000007
00000008
00000009
请按任意键继续. . . 
 */

 

以上是关于Convert PadLeft Bit Operate的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode] 405. Convert a Number to Hexadecimal_Easy tag: Bit Manipulation

PadLeft 补零

Java中实现String.padLeft和String.padRight

B - Bit++

使用任意字符串向左或向右填充 string.format(不是 padleft 或 padright)

String扩展 让你在PadLeft和PadRight时不再受单双字节问题困扰