Re: Comparing with 0.

From: Val (valmont_programming_at_hotmail.com)
Date: 02/21/05


Date: Mon, 21 Feb 2005 15:57:15 +0100


> If you want to compare whether a set of bits
> within an unsigned integer are zero, then
> look up "masking" in your text book.

Not "unsigned", but signed int :)
And yes, I did read alright. Understanding of the operators one by one is
not the problem.

> The steps are:
> 1. Create a "mask" containing the bit values
> for each bit you want to test.
> Example: 0xFF represents the "lowest" 8 bits.
>
> 2. Arithmetic AND the unsigned integer with the
> mask:
> result = value & 0xFF;
>
> 3. Test for zero.
> if ((value & 0xff) == 0) /*... */

This can't be right: Not even for unsigned. (compilable code below). "void
binary()" from B. Stroustroup by the way.

#include <iostream>
#include <cstdlib>
#include <bitset>

using namespace std;

void binary (int i )
{
  // assume 8 bit byte
  bitset<8*sizeof(int)> b = i ;
  cout << b << endl;
}

int main(int argc, char *argv[])
{
  int val = 256;
  binary(val);
  binary(val & 0xff);
  cin.get();
  return 0;
}

So there's a more sophisticated algo needed. A one that also takes INT_MIN
into account, since that is only zero's witht he right most bit set to 1.
But it is not a "0".
The challenge is only to use "~ & ^ | + << >> ".
NO "==". :)
You see the problem that I have now?
That's why I asked where "==" was defined, hoping to get a good hint on how
to do it :)



Relevant Pages

  • Re: Better use of random number genator bits?
    ... This on average is a total of about 2880 bits to shuffle a deck (of course ... Let's choose a random 32-bit unsigned integer and check if it is ... int i, j, n; ... randnum_UL = KISS; ...
    (sci.math)
  • Re: data types
    ... sizeof tells me there are 4 bytes to an int. ... Depending on what you are doing, you might want to consider as an alternative what is generally used where I used to work and generally called with "wrap angles" or BAMs. ... Unsigned integer types are guaranteed by the C standard to wrap. ... There are also various IDEs using gcc. ...
    (comp.lang.c)
  • Re: 2.3 -> 2.4: long int too large to convert to int
    ... long int too large to convert to int ... How do I tell Python that 0xc0047a80 is an ... somehow cobble together some way of forcing an unsigned integer into a ... signed integer. ...
    (comp.lang.python)
  • Re: questions about size_t
    ... size_t is an unsigned integer type large enough to hold the value returned from sizeof. ... The actual int types that correspond to size_t can vary. ... Should I change that to size_t if I need to compare the i ...
    (comp.lang.c)
  • Re: A problem about "vector" for c++ beginner
    ... unsigned integer, and ix is a signed int. ... Mixing the two (such as ...
    (alt.comp.lang.learn.c-cpp)