乱七八糟
程序员文章站
2024-03-25 08:09:52
...
记点乱七八糟的东西,免得自己忘了查起来麻烦
负数二进制
~0 = ~00000000 = (取反)11111111 = (-1)11111110 = (反码)10000001 = -1
负数即,二进制的第一位数代表符号位,0为正数,1为负数。
负数的反码,即符号位不动,其余取反。-5 = 10000101 = (反码)11111010
负数的补码,即反码+1。-5 = (反码)11111010 = (补码)11111011
系统中负数的二进制存的是补码,如:
int x = -17;
string str= Convert.ToString(x,2);
Debug.Log(str);
输出结果:
11111111111111111111111111101111
IEnumerable和IEnumerator
c#中,当一个类继承IEnumerable接口,或者类中实现IEnumerator GetEnumerator()方法,这个类即可使用foreach去遍历。
详情请见:https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.ienumerator?view=netframework-4.7.2
using System.Collections;
using UnityEngine;
public class Test : MonoBehaviour{
void Start(){
TextEach te = new TextEach(new string[] {"111", "222", "333", "444", "555", "666" });
foreach(var t in te) {
Debug.Log("t "+t);
}
}
}
public class TextEach : IEnumerable {
string[] array;
public TextEach(string[] arr) {
array = arr;
}
public IEnumerator GetEnumerator() {
return new TextEachEnum(array);
}
}
public class TextEachEnum : IEnumerator {
public string[] array;
int index = -1;
public TextEachEnum(string[] arr) {
array = arr;
}
public object Current {
get {
return array[index];
}
}
public bool MoveNext() {
index++;
return (index < array.Length);
}
public void Reset() {
index = -1;
}
}
下一篇: 自己乱七八糟的小总结