当我尝试解析JSON时,我收到一个非常奇怪的错误.实际上,文件非常简单,由一个简单的对象组成如下:
{
"registered":false,
"firstname":"xxx",
"name":"yyyy",
"email":"yyyy.xxx@gmail.com",
"picture":"xxxxx.jpg",
"username":"xxxy"
}
Run Code Online (Sandbox Code Playgroud)
为了解析这个文件,我使用了以下代码,其灵感来自Android SDK的示例:
public static boolean isRegistered(int nmb) {
boolean toReturn = true;
JsonReader reader = null;
try {
reader = new JsonReader(new InputStreamReader(new URL("xxx").openConnection().getInputStream()));
reader.beginObject();
while(reader.hasNext()) {
String name = reader.nextName();
Log.i("Next value", name);
switch (name) {
case "registered":
toReturn = reader.nextBoolean();
break;
case "firstname":
ProfileManager.getInstance().setFirstname(reader.nextString());
break;
case "name":
ProfileManager.getInstance().setName(reader.nextString());
break;
case "email":
break;
case "picture":
break;
case "username":
break;
}
}
reader.endObject();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch …Run Code Online (Sandbox Code Playgroud) 我对按位运算所需的周期数有疑问,或者更确切地说,是XOR运算.在我的程序中,我有两个uint8_t变量的一维数组,固定大小为8.我想对两个数组进行异或,我想知道最有效的方法是什么.这是一个总结我发现的选项的代码:
int main() {
uint8_t tab[4] = {1,0,0,2};
uint8_t tab2[4] = {2,3,4,1};
/* First option */
uint8_t tab3[4] = {tab[0]^tab2[0], tab[1]^tab2[1], tab[2]^tab2[2], tab[3]^tab2[3]};
/* Second option */
uint32_t* t = tab;
uint32_t* t2 = tab2;
uint32_t t3 = *t ^ *t2;
uint8_t* tab4 = &t3;
/* Comparison */
printf("%d & %d\n", tab3[0], tab4[0]);
printf("%d & %d\n", tab3[1], tab4[1]);
printf("%d & %d\n", tab3[2], tab4[2]);
printf("%d & %d\n", tab3[3], tab4[3]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
从循环/字节的角度来看,最佳选择是什么?