I have an email from a developer in which he says:
As you may know 1110000000000000 means 1+2+4
I won't be able to contact him for a few days. Can anyone else explain how that is possible?
Numbers appear to be turned into binary using the following function:
function toBinaryString(bitmask)
tvar2 = 0
tvar3 = 1
tvar1 = ""
do while tvar2 < 16
if (bitmask and tvar3) > 0 then
tvar1 = tvar1 & "1"
else
tvar1 = tvar1 & "0"
end if
tvar3 = tvar3 * 2
tvar2 = tvar2 + 1
loop
toBinaryString = tvar1
end function
It's little endian notation (Wiki). Basicaly the least significant bits appear on the left, unlike big endian notation (which is what most people think of when talking about binary).
As such the first bit represents 0^2, then 1^2, 2^2 etc. (so 1 + 2 + 4).
Prepare for some interesting reading material: How bytes work
Actually your developer is not correct, 1110000000000000 in binary notation is 57344 in decimal notation.
Related
I know that (10001010<<2) = 00101000
And that (10001010>>2) = 00100010
How to shift when I have only one bit like this
(1<<5) and (1>>5)
The type of 1 in C is int, which is always larger than 1 bit.
Note that right-shifting a signed quantity is implementation-defined, but I guess most would give 0 since there's just a single 1 present and it's gone after the first bit shift.
First, if you do ( 010101 << 1 ) then it will consider "010101" as a decimal number and not a binary. The notation "0bxxx" tells the compiler that your number is binary ( 0b010101 ).
For a single bit (your question), 1 decimal = 1 binary so you can use 1. However ( 1 >> anything ) should give you 0 all the time as you seem to know.
If you want to shift left, then this condition will return TRUE :
if( 8 == (1 << 3))
because (0b0001 << 3) = 0b1000 = 8
int isPower2(int x) {
int neg_one = ~0;
return !(neg_one ^ (~x+1));
}
This code works, I have implemented it and it performs perfectly. However, I cannot wrap my head around why. When I do it by hand, it doesn't make any sense to me.
Say we are starting with a 4 bit number, 4:
0100
This is obviously a power of 2. When I follow the algorithm, though, ~x+1 =
1011 + 1 = 1100
XORing this with negative one (1111) gives 0011. !(0011) = 0. Where am I going wrong here? I know this has to be a flaw in the way I am doing this by hand.
To paraphrase Inigo Montoya, "I do not think this does what you think it does".
Let's break it down.
~x + 1
This flips the bits of 'x' and then adds one. This is the same as taking the 2's complement of 'x'. Or, to put it another way, this is the same as '-x'.
neg_one ^ (~x + 1)
Using what we noted in step 1, this simplifies to ...
neg_one ^ (-x)
or more simply ...
-1 ^ (-x)
But wait! XOR'ing something with -1 is the same as flipping the bits. That is ...
~(-x)
~(-x)
This can be simplified even more if we make use of the 2's complement.
~(-x) + 0
= ~(-x) + 1 - 1
= x - 1
If you are looking for an easy way to determine if a number is a power of 2, you can use the following instead for numbers greater than zero. It will return true if it is a power of two.
(x & (x - 1)) == 0
I am looking at a C++ source code where there is what looks to be a simply calculation. Yet with no knowledge in C++, I can't quite understand how it is really calculated (my goal being making the same calculation under Excel).
uint64_t fpo = (((ui>>32) << 24) | (ui & 0xffffff)) + 0x123e00;
PrintAndLog("ui result : %lld [0x%llX]", ui, ui);
In my case, ui is 0x0F007057B9
and the result is 0xF8295B9
If I take the windows Calculator, I do (in Hex mode)
(0x0F007057B9 AND 0xffffff) + 0x123e00
I am getting the same result (expect for the first 0xF)
I have read somewhere that AND returns 1 if both bits of same "weigh" are 1
So if I take ui AND 0xffffff converted to Binary :
0x0F007057B9 0000111100000000011100000101011110111001
0xffffff 111111111111111111111111
-------------------------------------------------------
0000111100000000011100000101011110111001
Which does make any change...
Am I completely going to wrong way?? Sorry for my poor english, I am from South Korea.
Thank you for helping me {^^;
For AND its
0 AND 0 = 0
0 AND 1 = 0
1 AND 0 = 0
1 AND 1 = 1
So if you AND a number with 0xffffff, you basicly just keep the numbers at those positions. You fill in the missing ones with 0.
In your case:
0x0F007057B9 0000111100000000011100000101011110111001
0xffffff 0000000000000000111111111111111111111111
-------------------------------------------------------
0000000000000000011100000101011110111001
Im not sure, if that was your question tho :)
I'm trying to determine if a bitstring, say 64 bit long, is at least 50% ones. I've searched around and looked at the great http://graphics.stanford.edu/~seander/bithacks.html, but I haven't found anything specifically for this problem.
I can split the string up into 8bit chunks, pre-calculate the number of 1s in each, and then find the result in 8 lookups and 7 additions.
Example of bytewise approach:
10001000 10000010 00111001 00001111 01011010 11001100 00001111 11110111
2 + 2 + 4 + 4 + 4 + 4 + 4 + 7 = 31
hence 0 dominates.
I just feel like there must be a better way given I just want to find the dominator. Maybe I'm just using the wrong name?
You can use the divide and concur solution here, which is easy adaptable to 32-bit. Or maybe just a popcnt instruction depending on your hardware. Then you just check if that value is less than 32, if so 0s dominate, otherwise 1s dominate.
The code from the link adapted to 64-bit and with the domination logic inserted:
(I've bit shifted right by an extra 5 bits at the end to check if the set bits is greater than 31 in the same shift)
int AtLeastHalfOnes(long long i) {
i = i - ((i >> 1) & 0x5555555555555555LL);
i = (i & 0x3333333333333333LL) + ((i >> 2) & 0x3333333333333333LL);
return (((i + (i >> 4)) & 0x0F0F0F0F0F0F0F0FLL) * 0x0101010101010101LL) >> 61;
}
I think it is better to use Stack data structure. When your input bit is 1, then push(1);. Otherwise pop(); from top of your stack. Finally if your stack is not empty, I think your problem solved.
This isn't a regular "binary to bcd" question, in fact, I'm not really sure what to call the thing I'm trying to do!
There is a single byte in an embedded device that stores the numbers 1 through 7 (for days of the week) in the following format:
00000001 = 1
00000010 = 2
00000100 = 3
00001000 = 4
00010000 = 5
00100000 = 6
01000000 = 7
I want to read this byte, and convert its contents (1 through 7) into BCD, but I'm not sure how to do this.
I know I could just brute-force it with a series of if statements:
if(byte == B00000001)
{
answer = 1;
}
else
if(byte == B00000010)
{
answer = 2;
}
and so on, but I think there could be a better way. This data is stored in a single register on a real time clock. I'm getting this byte by performing an I2C read, and I read it into a byte in my program. The datasheet for this real-time clock specifies that this particular register is formatted as I have outlined above.
You can use a lookup table...
/* this is only needed once, if lut is global or static */
unsigned char lut[65];
lut[1]=1;
lut[2]=2;
lut[4]=3;
lut[8]=4;
lut[16]=5;
lut[32]=6;
lut[64]=7;
...
...
...
/* Perform the conversion */
answer = lut[byte];
Or you can even use some math...
answer = 1 + log(byte)/log(2);
If this is being compiled on an ARM processor, you can simply do this:
result = 31 - __CLZ(number);
Assuming number is a 32-bit one-hot > 0.
You can make use of bitwise and modulo operations to do this efficiently without needing to create a large array
for (int answer = 1; (byte % 2) == 0; ++answer) {
byte >>= 1;
}
(I know this was an old question, I just wanted to share because this was a high Google result for me)