bit-representation

Can pointers to different types have different binary representations?

只愿长相守 提交于 2021-02-18 06:29:51
问题 I wonder if C++ implementations are allowed to represent pointers to different types differently. For instance, if we had 4-byte sized/aligned int and 8-byte sized/aligned long , would it be possible to represent pointers-to- int / long as object addresses shifted right by 2/3 bits, respectively? This would effectively forbid to convert a pointer-to- long into a pointer-to- int . I am asking because of [expr.reinterpret.cast/7]: An object pointer can be explicitly converted to an object

How to check whether an int variable contains a legal (not trap representation) value?

痞子三分冷 提交于 2020-01-12 23:32:08
问题 Context: This is mainly a followup to that other question. OP wanted to guess whether a variable contained an int or not, and my first thought was that in C (as in C++) an int variable could only contain an int value. And Eric Postpischil reminded me that trap representations were allowed per standard for the int type... Of course, I know that most modern system only use 2-complement representations of integers and no padding bits, meaning that no trap representation can be observed.

How to check whether an int variable contains a legal (not trap representation) value?

这一生的挚爱 提交于 2020-01-12 23:31:24
问题 Context: This is mainly a followup to that other question. OP wanted to guess whether a variable contained an int or not, and my first thought was that in C (as in C++) an int variable could only contain an int value. And Eric Postpischil reminded me that trap representations were allowed per standard for the int type... Of course, I know that most modern system only use 2-complement representations of integers and no padding bits, meaning that no trap representation can be observed.

How To Represent 0.1 In Floating Point Arithmetic And Decimal

旧时模样 提交于 2019-12-16 20:59:41
问题 I am trying to understand floating point arithmetic better and have seen a few links to 'What Every Computer Scientist Should Know About Floating Point Arithmetic'. I still don't understand how a number like 0.1 or 0.5 is stored in floats and as decimals. Can someone please explain how it is laid out is memory? I know about the float being two parts (i.e., a number to the power of something). 回答1: I've always pointed people towards Harald Schmidt's online converter, along with the Wikipedia

ASCII table and character presentation

假装没事ソ 提交于 2019-12-13 05:07:55
问题 We learn in class about ASCII table, and that every character from 128 characters has a unique number from 0-128 representing it. For example "a" is 97 (in binary 97 is 1100001). "%" is 37 (and in binary 37 is 0100101). (I understand that for a fixed length of 7 we should allow the binary number start with 0) If 97 is representing "a", then what represents the string "97"? What represents the integer 97? 回答1: I think your question is based on the notion that, given a representation of an

Rounding floats with representation error to the closest and correct result

我只是一个虾纸丫 提交于 2019-12-01 04:11:38
I have a situation when the classic representation error in Python started to be a problem: I need them to be used for Matrix operations in Numpy and the decimal type is not supported yet. You all know that if I do 111.85 * 111.85 I will get 12510.422499999999 but if I round(12510.422499999999, 4) I can get the proper result which is of course 12510.4225 . But the actual questions are: Is this round stuff a good idea and a good practice? Will this work for all cases? sometimes the decimals position where the ..999 decimals could be more And finally, How to get the appropriate number of decimal

Rounding floats with representation error to the closest and correct result

杀马特。学长 韩版系。学妹 提交于 2019-12-01 01:35:44
问题 I have a situation when the classic representation error in Python started to be a problem: I need them to be used for Matrix operations in Numpy and the decimal type is not supported yet. You all know that if I do 111.85 * 111.85 I will get 12510.422499999999 but if I round(12510.422499999999, 4) I can get the proper result which is of course 12510.4225 . But the actual questions are: Is this round stuff a good idea and a good practice? Will this work for all cases? sometimes the decimals