c++ - Convering Big Endian Formatted Bits to Intended Decimal Value While Ignoring First Bit -
i reading binary file , trying convert ibm 4 byte floating point double in c++. how 1 use first byte of ibm data find ccccccc in given picture
the code below gives exponent way larger data should have. confused how line
exponent = ((ibm4bytevalue[0] & 127) - 64);
executes, not understand use of & operator in statement. previous author of code implied (ibm4bytevalue[0]) ccccccc , mean ampersand sets maximum value left side of operator can equal? if correct though i'm sure how line accounts fact there big endian bitwise notation in first byte (i believe big endian after viewing picture). not mention 1000001 , 0000001 should have same exponent (-63) not current interpretation of mentioned line.
so in short show me how find ccccccc (shown in picture link above) using first byte --> ibm4bytevalue[0]. maybe accessing each individual bit? not know code using array.
**this code using std namespace **i believe ret should mantissa * pow(16, 24+exponent) if i'm wrong exponent i'm probable wrong (i got ibm conversion asked stackoverflow question) **i have commented on old post, question bit large, pun intended, comment. different in asking how 1 accesses bits in array storing whole bytes.
code put using ibm conversion previous question answer
for (long pos = 0; pos < filelength; pos += buf_len) { file.seekg(byteposition); file.read((char *)(&ibm4bytevalue[0]), buf_len); byteposition += 4; printf("\n%8ld: ", pos); //ibm conversion double ret = 0; uint32_t mantissa = 0; uint16_t exponent = 0; mantissa = (ibm4bytevalue[3] << 16) | (ibm4bytevalue[2] << 8)|ibm4bytevalue[1]; exponent = ((ibm4bytevalue[0] & 127) - 64); ret = mantissa * exp2(-24 + 4 * exponent); if (ibm4bytevalue[0] & 128) ret *= -1.; printf(":%24f", ret); printf("\n"); system("pause");
}
the & operator takes bits in value of array , masks binary value of 127. if bit in value of array 1, , corresponding bit position of 127 1, bit resulting 1. 1 & 0 0, , 0 & 0 , , 0 & 1. changing bits. take resulting bit value, converted decimal now, , subtract 64 equal exponent.
in floating point have bias (in case, 64) exponent. means if exponent 5, 69 stored. code trying find original value of exponent.
Comments
Post a Comment