File: BitInfo.cs

package info (click to toggle)
hexbox 1.5.0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid, stretch
  • size: 2,440 kB
  • ctags: 1,210
  • sloc: cs: 6,784; makefile: 21; sh: 1
file content (76 lines) | stat: -rw-r--r-- 1,342 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
´╗┐using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace Be.HexEditor
{
	public class BitInfo
	{
		private byte _value;

		public byte Value
		{
			get { return _value; }
			set { _value = value; }
		}

		public long Position { get; set; }

		public BitInfo(byte value, long position)
		{
			_value = value;
			Position = position;
		}

		public override string ToString()
		{
			var result = string.Format("{0}{1}{2}{3}{4}{5}{6}{7}"
				, GetBitAsString(7)
				, GetBitAsString(6)
				, GetBitAsString(5)
				, GetBitAsString(4)
				, GetBitAsString(3)
				, GetBitAsString(2)
				, GetBitAsString(1)
				, GetBitAsString(0)
				);
			return result;
		}

		public string GetBitAsString(int index)
		{
			if (this[index])
				return "1";
			else
				return "0";
		}

		public bool this[int index]
		{
			get
			{
				return (_value & (1 << index)) != 0;
			}
			set
			{
				if (value)
					_value |= (byte)(1 << index); //set bit index 1
				else
					_value &= (byte)(~(1 << index)); //set bit index 0
			}
		}

		byte ConvertToByte(BitArray bits)
		{
			if (bits.Count != 8)
			{
				throw new ArgumentException("bits");
			}
			byte[] bytes = new byte[1];
			bits.CopyTo(bytes, 0);
			return bytes[0];
		}

	}
}