problem about bits-Collection of common programming errors
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 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…OLDEST 1 1 0 1 0 NEWESTSo what is the best
Shafik Yaghmour
c++ bits bit-fields
Would 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’ve tried sizeof(test.one) but which clearly won’t work. I realize this is a measure of bits:#include <iostream>using namespace std;int main() {struct mybits {unsigned int one:15;};mybits test;test.one = 455;cout << test.one << endl;cout << “The size of test.one is: ” << sizeof
Charles
c# byte bits
I have a variable of type sbyte and would like to copy the content to a byte. The conversion wouldn’t be a value conversion, rather a bit per bit copy.For example,if mySbyte in bits is: ‘10101100’, after conversion, the corresponding byte variable will also contain the bits ‘10101100’.
Ravi Gupta
c++ bits bitmask
I have this function and apparently it causes my program to crash:long long todos(long long x,long long i) {x ^= (1 << i);long long aux = i – 1;if(aux >= 0) x ^= (1 << aux);aux = i – 4;if(aux >= 0) x ^= (1 << aux);aux = i + 1;if(aux < 16) x ^= (1 << aux);aux = i + 4;if(aux < 16) x ^= (1 << aux);return x; }What I don’t understand is why when I change all the ^= ( for &= ~( it runs perfectly fine (although the output I am getting is different). Is ther
Kirby
java data bits bitset bitsets
The Java reference here indicates that boolean types, while represented with a “bit” 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 “size” of a BitSet is undefined – it’s composed of boolean values, after all. And sure enough, the do
user1386132
c arrays vector bits
I’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’re allowed to assume that the range for the elements in the array is 1-9. Here’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 < nValues; i++){(57)set[i] =
curiousguy
c++ floating-point bits strict-aliasing type-punning
I 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*)&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*)&x;unsigned* u = (unsigned*)c;return *u; }Or do I have to extract the individual bytes myself?unsigned baz(float x) {unsigned
leppie
c bits
I looked around for quite a bit and I can’t seem to find the answer to this seemingly simple question: what exactly IS “+” or “-” in C (bits)?For example, what is the representation when 1 is added to 11111111 11111111 11111111 11111111?I ask this because I’m reading through some code and I don’t know what ~0 + 1is doing. I mean, we can’t add 1 to 4294967295 right?Thanks!
pauliwago
bit-shift bits
I am working on bit shifts, and I’ve run into this problem. I have two int:int x = 1; int y = 2;What is the difference between:x = x << (31 + 1);andy = y << 31;I thought the result would be the same (namely that x and y would both equal 1) , but they aren’t…..I don’t understand why. 2 is just 1 with a “1” bit moved one space to the left.Thanks!I mean when we can’t shift-left anymore, don’t we wrap around to the beginning?EDIT: Let me clarify what I THINK is going on:We start with
kubiej21
c void-pointers bits
If 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(“0x%x\n”, (int *)p);I would like the above to print the 24 bits that I just allocated.
Ryan
c++ visual-c++ gcc microsoft bits
From 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
Web site is in building