decimal = (bA[7] * 128) + (bA[6] * 64) + (bA[5] * 32) + (bA[4] * 16) +(bA[3] * 8)+ (bA[2]*4) +(bA[1]*2)+ bA[0];
Given the array: booleanArray = {1,0,1,0,1,0,1,0};
The OP wants the corresponding decimal number which is , by manual calculation, 170.
The quoted expression, upon manual computation, gives:
1128 + 064 + 132 + 016 + 18 + 04 + 12 + 00 = 128 + 32 + 8 + 2 = 170
The quoted expression, upon computer based computation, gives:
byte x = 0
x = x + 128; //x = 0x80 ; because the CPU will do binary addition
x = x+ 32; //x= 0x80 + 32 = 0xA0
x = x + 8; //x = 0xA0 = 8 = 0xA8
x = x+2; //x = 0xA8 + 2 = 0xAA
I am back to 10101010 (0xAA); where is the expected 170! ![]()
So, trick to obtain decimal (aka BCD) value by adding the respective positional values (positional factor*positional weight) will fail unless provision is kept for the necessary adjustment while the partial decimal (aka BCD) result is about to switch from 9 to 10 (9 + 1 ----> A). The 8085 has DAA, the 80x86 has DAA, the 8051 has DA A, and the ATmega has none for this adjustment.