{"id":4210,"date":"2014-03-30T09:18:03","date_gmt":"2014-03-30T09:18:03","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/problem-about-bits-collection-of-common-programming-errors\/"},"modified":"2014-03-30T09:18:03","modified_gmt":"2014-03-30T09:18:03","slug":"problem-about-bits-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/problem-about-bits-collection-of-common-programming-errors\/","title":{"rendered":"problem about bits-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/87bf1ed23a3445270123aedd243050b8?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nuser620189<br \/>\nc++ list boolean bits<br \/>\nI need to keep track of n samples. The information I am keeping track of is of boolean type, i.e. something is true or false. As soon as I am on sample n+1, i basically want to ignore the oldest sample and record information about the newest one.So say I keep track of samples, I may have something likeOLDEST 0 0 1 1 0 NEWESTIf the next sample is 1, this will becomeOLDEST 0 1 1 0 1 NEWESTif the next one is 0, this will become&#8230;OLDEST 1 1 0 1 0 NEWESTSo what is the best<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/db869d4e8c636d15bd18678f6ced444b?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nShafik Yaghmour<br \/>\nc++ bits bit-fields<br \/>\nWould anyone know how to extract the size of a bit-field member. The below code naturally gives me the size of an integer, but how do I find out how many bits or bytes are in mybits.one? I&#8217;ve tried sizeof(test.one) but which clearly won&#8217;t work. I realize this is a measure of bits:#include &lt;iostream&gt;using namespace std;int main() {struct mybits {unsigned int one:15;};mybits test;test.one = 455;cout &lt;&lt; test.one &lt;&lt; endl;cout &lt;&lt; &#8220;The size of test.one is: &#8221; &lt;&lt; sizeof<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/b6116ac3c4b5835c6745570ea73a85c1?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nCharles<br \/>\nc# byte bits<br \/>\nI have a variable of type sbyte and would like to copy the content to a byte. The conversion wouldn&#8217;t be a value conversion, rather a bit per bit copy.For example,if mySbyte in bits is: &#8216;10101100&#8217;, after conversion, the corresponding byte variable will also contain the bits &#8216;10101100&#8217;.<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/7744ae61f644a4c5ca9f3d1e2c44340c?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nRavi Gupta<br \/>\nc++ bits bitmask<br \/>\nI have this function and apparently it causes my program to crash:long long todos(long long x,long long i) {x ^= (1 &lt;&lt; i);long long aux = i &#8211; 1;if(aux &gt;= 0) x ^= (1 &lt;&lt; aux);aux = i &#8211; 4;if(aux &gt;= 0) x ^= (1 &lt;&lt; aux);aux = i + 1;if(aux &lt; 16) x ^= (1 &lt;&lt; aux);aux = i + 4;if(aux &lt; 16) x ^= (1 &lt;&lt; aux);return x; }What I don&#8217;t understand is why when I change all the ^= ( for &amp;= ~( it runs perfectly fine (although the output I am getting is different). Is ther<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/396823ca07b467b395f720884ab16b5b?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nKirby<br \/>\njava data bits bitset bitsets<br \/>\nThe Java reference here indicates that boolean types, while represented with a &#8220;bit&#8221; of information, do not have a precisely defined size. In contrast, other types seem to suggest that the size is defined. For example, an int is 32-bits, end of story.When we look at the spec for a BitSet, we can see that it is composed of boolean values. By the reference above, this seems to suggest that the &#8220;size&#8221; of a BitSet is undefined &#8211; it&#8217;s composed of boolean values, after all. And sure enough, the do<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/e78613431cc1c680fda9de25362067e0?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nuser1386132<br \/>\nc arrays vector bits<br \/>\nI&#8217;m trying to create a bit vector set from a given array. Not sure how to start it off. For example given the array: int rows[] = {1, 2, 5} I need to make a function unsigned short MakeBitVector(int values[], int nValues) You&#8217;re allowed to assume that the range for the elements in the array is 1-9. Here&#8217;s what I have so far:unsigned short MakeBitVector(int values[], int nValues) {(55)unsigned short int set = calloc(nValues, sizeof(unsigned short));for(int i = 0; i &lt; nValues; i++){(57)set[i] =<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/94e2dd813e278309b6281b4a4b7fdddd?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\ncuriousguy<br \/>\nc++ floating-point bits strict-aliasing type-punning<br \/>\nI am trying to extract the bits from a float without invoking undefined behavior. Here is my first attempt:unsigned foo(float x) {unsigned* u = (unsigned*)&amp;x;return *u; }As I understand it, this is not guaranteed to work due to strict aliasing rules, right? Does it work if a take an intermediate step with a character pointer?unsigned bar(float x) {char* c = (char*)&amp;x;unsigned* u = (unsigned*)c;return *u; }Or do I have to extract the individual bytes myself?unsigned baz(float x) {unsigned<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/515ca9736a9a8ce65e58af764ccddf46?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nleppie<br \/>\nc bits<br \/>\nI looked around for quite a bit and I can&#8217;t seem to find the answer to this seemingly simple question: what exactly IS &#8220;+&#8221; or &#8220;-&#8221; in C (bits)?For example, what is the representation when 1 is added to 11111111 11111111 11111111 11111111?I ask this because I&#8217;m reading through some code and I don&#8217;t know what ~0 + 1is doing. I mean, we can&#8217;t add 1 to 4294967295 right?Thanks!<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/ba0451de9f74bef13bf0cdf844da0992?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\npauliwago<br \/>\nbit-shift bits<br \/>\nI am working on bit shifts, and I&#8217;ve run into this problem. I have two int:int x = 1; int y = 2;What is the difference between:x = x &lt;&lt; (31 + 1);andy = y &lt;&lt; 31;I thought the result would be the same (namely that x and y would both equal 1) , but they aren&#8217;t&#8230;..I don&#8217;t understand why. 2 is just 1 with a &#8220;1&#8221; bit moved one space to the left.Thanks!I mean when we can&#8217;t shift-left anymore, don&#8217;t we wrap around to the beginning?EDIT: Let me clarify what I THINK is going on:We start with<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/8a1abe5f6efeaa48401d14178e27e28a?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nkubiej21<br \/>\nc void-pointers bits<br \/>\nIf I create a void pointer, and malloc a section of memory to that void pointer, how can I print out the individual bits that I just allocated?For example:void * p; p = malloc(24); printf(&#8220;0x%x\\n&#8221;, (int *)p);I would like the above to print the 24 bits that I just allocated.<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/8d69da0263a53dc51dcd53dae788ef1b?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nRyan<br \/>\nc++ visual-c++ gcc microsoft bits<br \/>\nFrom MSDN Search the mask data from most significant bit (MSB) to least significant bit (LSB) for a set bit (1).unsigned char _BitScanReverse (unsigned long * Index,unsigned long Mask );Parameters [out] Index Loaded with the bit position of the first set bit (1) found.[in] Mask The 32-bit or 64-bit value to search.Return Value 0 if the mask is zero; nonzero otherwise.Remarks If a set bit is found, the bit position of the first set bit found is returned in the first parameter. If no set bit is fo<\/li>\n<\/ul>\n<p>Web site is in building<\/p>\n","protected":false},"excerpt":{"rendered":"<p>user620189 c++ list boolean bits I need to keep track of n samples. The information I am keeping track of is of boolean type, i.e. something is true or false. As soon as I am on sample n+1, i basically want to ignore the oldest sample and record information about the newest one.So say I [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4210","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4210","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4210"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4210\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}