问题
I was trying to set 46th bit in a register which of 64 bits wide using C.How do i go about setting this bit ?
Currently i am doing this:
uint32_t= address ;
uint64_t data =1ULL << 46;
Printing this is showing that bit 14 is getting set.I am not able to set even bit 32. If i set bit 32 it sets bit 0. 33 will set bit 1. Looks like it is doing circular shifting after 0-31 again it starts over with 0.
Register in 64 bit wide.
Any idea how do i go about setting this bit ?
Eg:
reg_addr.val = FEATURE_REG;
printf(stdout, "Programming enable at address %x=%llx\n",
reg_addr.val,reg_addr.val);
data.val = (1ULL << 46);
printf("Data value %llx\n",data.val);}
回答1:
If you use types as uint32_t or uint64_t printing correctly is done with:
printf(stdout, "Programming enable at address %" PRIu32 "=%" PRIu64 "\n",reg_addr.addr, reg_addr.val);
assuming reg_addr.addr is of type uint32_t and type reg_addr.val is of uint64_t.
回答2:
Your code is correct.
You should check that 64-bit integer types are correctly supported in your platform:
printf("%zu %zu\n", sizeof 1ULL, sizeof data);
should print
8 8
If this is the case, the error is probably (as mentioned by @pmg in the comments) in how you check if the bit is set.
In the new edit, you mentioned it is a register. IO registers can have special behavior due to their volatile property. I suggest you to first check to set a bit with a normal object.
回答3:
I hope you do not check the output of this line to conclude the shifting is not done correctly!?
printf(stdout, "Programming enable at address %x=%llx\n", reg_addr.val,reg_addr.val);
Passing the 64 bit value two times to printf, but using %x (probably 32 bit) and %llx (64 bit) to output the values will not work. I guess you meant to take the address of reg_addr.val at the first parameter!?
printf(stdout, "Programming enable at address %x=%llx\n", ®_addr.val,reg_addr.val);
来源:https://stackoverflow.com/questions/9207611/how-to-set-45th-bit-in-a-64-bit-register-field