= (~0); What does it mean? [duplicate] - c

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Is it safe to use -1 to set all bits to true?
int max = ~0; What does it mean?
Hello,
I have stumbled upon this piece of code..
size_t temp;
temp = (~0);
Anyone knows what it does?

~ is the bitwise not operator, it inverts each bit of the operand. In this case the operand is 0, so every bit is initially 0, and after applying the bitwise not every bit will be 1. The end result is you get a size_t filled with 1 bits.

sharptooth's answer is correct, but to give you more detail, the ~ is a binary operator for NOT. Basically, you're assigning the binary equivalent of NOT 0 to temp and that will set every bit to 1.

That's one way typically used to assign a size_t value built of all binary ones independent of actual size of size_t type. If that's the purpose of that code one should instead use (size_t)( -1 ).
Btw here's an identical question.

How about this?
C++ code:
#include <limits>
std::size_t temp = std::numeric_limits<std::size_t>::max();
C code:
Please take a look the question.
I think it is more proper way.

Related

Isolating Bits With Bitwise AND in C [duplicate]

This question already has answers here:
Understanding the bitwise AND Operator
(4 answers)
Closed 4 years ago.
I'm new to programming and have a question I was hoping I could get some help with.
I have a binary value 0100 0001 0000 0001 which has been assigned to a variable name valhex. I'm supposed to use the bitwise AND operator to make bits 13 through 3 KEEP their current value and ALL other bits are set to zero, then store the result back into the variable valhex. I'm supposed to do this using only one line of C code.
So far all I have is this:
unsigned int valhex = valhex&0000000100000000;
I know this isn't right but this is as far as I can get. I don't know where to put the & symbol in relation to the variable and the binary. I'm also not sure if I'm doing the right thing by making bits 0,1,2,14,15 zeroes. I thank you in advance for any help you might be able to give me.
In bitwise AND (if you recall your truth tables), bits that are ANDed with 1 keep their value, bits that are ANDed with a 0, are set to 0. So if you want to keep bits 13-3, your mask needs to have 1s in position 13-3, and 0s in position 2-0. Also note that to specify a binary literal, you need to prefix it with 0b. Also note that you can not declare and use the variable on the same line because it's uninitialized. The end result is this:
unsigned int valhex = 12345; /* some value */
valhex = valhex & 0x3ff8; /* 0x3ff8 = 0b11111111111000 */
Note that unsigned int is longer than 14 bits and you didn't specify what is supposed to happen to bits in position 14 and up. In this case, they'll be set to 0s as well.

What doe this C code do with (unsigned) and (long) cast [duplicate]

This question already has answers here:
!! c operator, is a two NOT?
(4 answers)
What is "!!" in C? [duplicate]
(7 answers)
Closed 8 years ago.
doing some exam prep and this is a past question.
Describe what the following pieces of C do and re write in a simple programming style with the same functionality.
(The bad indentation was the intention of the question).
With regards to section A i'm not sure what unsigned cast is doing to a. I have tested it a few times and can't seem to get a result that makes sense.
Similarly In B im not sure what how the while loop is working with the long cast and the !! being another problem
The Code :
//code section A
int f(int a,int b){
return(((unsigned) a)>b);}
//code section B
int h(int *x, int y){
int * z= x-- +y;
w=0;
while( (long) z-- ^(long) x) w += !!(*z);
return w;}
Any help would be appreciated, Thank you.
!! negates a boolean expression twice, essentially converting an expressions value to 0 or 1.
As in C all values other than zero mean true, and zero means false, !! can be used to convert it into 0 or 1, in the case you need to use it later in a function or expression which doesn't accept any value for true, only the number 1.
About the rest: unsigned interprets the internal representation of your int a from your function argument to unsigned int, so for example -1 becomes 4294967295 if your compiler uses two's complement and 4 byte ints.
About the casting to long : I strongly recommend against in similar situations unless you absolutely know what you are doing. In your example, it does some pointer arithmetic, in interpreting your pointer as numeric values, essentially working with the addresses of your variables as if they were just numbers. They have probably chosen long because on their system it had the exact same size as a pointer. This is not guaranteed to be so on all systems.
So, to give a very short answer to your question: The code does undefined behavior with those expressions, except for the !! which just give 0 if the expression was zero, and 1 otherwise.
The operator ! is the logical negation.
!true is false (0), and !false is true (1).
When a value is used as a boolean value, anyhting other than 0 (0 in a large sense) is true; 0 is false.
So, !!(*z) has either the value 0 or 1.
It will be 0 if *z was NULL to begin with
It will be 1 if *z was not NULL.
! is the boolean operator for not.
false is 0, and true is 1 in C.
So when you take any int, which is not 0- and you run ! on it, you'll get 0 (true becomes false), and zero will become one .
So the action of !! is changing each non-zero value to 1 , and leaving each 0 a 0.

Division by 3 without division operator

I was given this question in an interview to describe the output in comments.
unsigned int d2(unsigned int a)
{
__int64 q = (__int64)a * 0x0AAAAAAAB; // (2^33+1) / 3
return (unsigned int)(q >> 33);
}
I have checked other questions in Stackoverflow related to division by 3 but none seems so fast and small.
Can anybody help me in explaining how the function is giving the output written in comments ?
The function divides a 32-bit unsigned number by 3.
If you multiply by 2^33 and then divide by 2^33 (by right shifting), then you get the original number. But if you multiply by (2^33)/3 and then divide by 2^33, you effectively divide by three.
The last digit is B instead of A to cause the result to be rounded up.
There is no need to actually write this in your code because the compiler will usually do this for you. Try it and see. (Furthermore, for a signed input, the compiler can safely generate a signed right shift, but the C language does not define such an operation.)

Divide a number by 5 without using division operator [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
implement division with bit wise operator
Divide a number by 3 without using *, /, +, -, % operators
I came across this question in an interview. I want to know if there any possibly way to divide a number by 5 without using division operator and if any possible solution exists using bitwise operators only.I figured one out using repeated subtraction till zero approaches. Number can be signed and unsigned.
Please suggest any way out without using +,-,/,* and %.
My first idea was to just multiply by 0.2 (but I don't know a solution for how to implement that using bitwise operators from the top of my head).
Simply reduce division to subtracting one number from the other until you reach zero :D
int number = 25;
int divisor = 5;
int result = 0;
while((number-divisor)>=0){
result++;
number = number - divisor;
}
I seem to have found a way out from this link which seems to provide an answer to my question.
http://codegambler.wordpress.com/2009/08/11/division-operation-without-using-division-operator/

significance of int a : 2 syntax [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does ‘unsigned temp:3’ mean?
I came across one syntax like int variable:4;
can anyone tell me what this syntax means?
struct abc
{
int a;
int b:2;
int c:1;
};`enter code here`
It defines a width of a bitfield in a struct. A bitfield holds an integer value, but its length is restricted to a certain number of bits, and hence it can only hold a restricted range of values.
In the code you posted, in the structure a is a 32-bit integer, b is a 2-bit bitfield and c is a 1-bit bitfield.
It is a bit-field. Instead of storing a full integer for b it only stores 2 bits, so b can have values of -2, -1, 0 and 1. Similarly c can only have values of -1 and 0.
Depending on what version of the compiler you have the sign extension is a little unpredictable, and some systems may present these values as 0, 1 2 and 3 or 0 and 1.
This will also pack the fields into less than an integer, but again, this is in an implementation defined manner, and you would do well not to make assumptions about how much memory will actually be used or the order of the data in memory.

Resources