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

namespace BPASmartClient.KLMCoffee.Protocal
{
    public class BitValue
    {
        #region Byte位数据获取
        /// <summary>
        /// 7 6 5 4 3 2 1 0
        /// </summary>
        /// <param name="value"></param>
        /// <param name="bit"></param>
        /// <returns></returns>
        public bool GetBitValue(byte value,byte bit)
        {
            return (value & (byte)Math.Pow(2,bit)) > 0 ? true : false;
        }

        /// <summary>
        /// 设置某一位的值
        /// </summary>
        /// <param name="data"></param>
        /// <param name="index">要设置的位， 值从低到高为 1-8</param>
        /// <param name="flag">要设置的值 true / false</param>
        /// <returns></returns>
        public byte SetBitValue(byte data,int index,bool flag)
        {
            if (!((index > 8 || index < 1)))
            {
                int v = index < 2 ? index : (2 << (index - 2));
                return flag ? (byte)(data | v) : (byte)(data & ~v);
            }
            else
                return data;
        }
        #endregion
    }
}
