This question already has answers here:
Confusing operator precedence: a << b + c << d
(4 answers)
Closed 8 years ago.
when am I applying left shifting to 1 by 4 bit as 1<<4 it printing 16 as the value but if I will apply shifting like 1<<4 + 1<<3 then it printing 256 as a result,
I am not getting, how many shifting is apply and how it is working?
but according to me answer should be 24 by applying 4 left shift + 3 shift
1 << 4 + 1 << 3
Is actually interpreted as
(1 << ( 4 + 1 )) << 3
See?
1 << 5 --> 32
32 << 3 --> 256
When in doubt, use more parentheses!
(1 << 5) + (1 << 3) --> 24
Related
when
1<<2 : 4
4<<1<<2 should be 4<<4 which is 64
but it is showing 32.
I am new to bit manipulation, please let me know where I am doing wrong.
The expression is evaluated from left to right.
4 << 1 << 2
is equivalent to
(4 << 1) << 2
which is the same as
8 << 2
which equals
32
This statement checks whether a number is 32 bits.
0 < res <= (1 << 31) -1
I can't seem to understand how, can someone help understand this bit shift syntax?
Well, lets begin with an example:
1 in binary is 1
2 in binary is 10
4 in binary is 100
We can see that we need to 'add' an 0 at the end of each number to multiply by 2 and in most language we can do this with this syntax: number << 1
Here we are saying that we add a 1 time a 0 to the left. number >> 1 and here we add 1 time a 0 to the right.
So 1 << 31 means 1 * 2 * 2 * 2 ... 31 times which means 2^31 (so 32 bits)
This question already has answers here:
What does the vertical pipe ( | ) mean in C++?
(4 answers)
Closed 4 years ago.
Having the follow code -
enum FileOpenFlags {
flagREAD = 1, flagWRITE = 2,
flagCREATE = 4, flagEND = 8,
flagAPPEND = flagWRITE | flagEND,
};
cout << flagAPPEND << endl;
gives 10 . can you explain me what the | did ?
It did a bitwise or of the two values.
flagWRITE's (2) binary representation is 0010
flagEND's (8) binary representation is 1000
0010 OR 1000 gives you 1010 which equals 10
It's called Bitwise OR........
It's a bitwise "OR" operator. So the bit value of 2 and 8 respectively get OR'd bitwise.
So:
1000 (flagEND = 8)
OR 0010 (flagWRITE = 2)
-----------
= 1010 (flagAppend = 10)
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
why is initializing an integer in VC++ to 010 different from initialising it to 10?
This got me very confused, and I hope one of you can answer my question. How come this code will produce the output "116"?
#include <iostream>
int main()
{
std::cout << 0164 << std::endl;
return 0;
}
The code has been compiled with MSVC++ 2010 and g++ under Mac OS X.
"cout" can print '0' alone and '164' alone, but as soon '0' is the first digit in the number the output changes.
Because the 0 in front makes the number be interpreted as octal.
0164 =
4 * 1 +
6 * 8 +
1 * 64
= 116
Or, via binary:
0164 =
0 1 6 4 =
000 001 110 100 =
1110100 =
116
The same goes for hexadecimal numbers, you write them as 0x1FA for example.
In C and its brethren, a number with 0 on the front is octal, not decimal.
Hence your number is 1 * 82 (1 * 64 = 64) plus 6 * 81 (6 * 8 = 48) plus 4 * 80 (4 * 1 = 4) which equates to 116.
See here for a large treatise on what hexadecimal and octal are in C.
Can someone explain to me what this does?
#define ROUNDUP(n,width) (((n) + (width) - 1) & ~unsigned((width) - 1))
Providing width is an even power of 2 (so 2,4,8,16,32 etc), it will return a number equal to or greater than n, which is a multiple of width, and which is the smallest value meeting that criteria.
So width = 16; 5->16, 7->16, 15->16, 16->16, 17->32, 18->32 etc.
EDIT I started out on providing an explanation of why this works as it does, as I sense that's really what the OP wants, but it turned into a rather convoluted story. If the OP is still confused, I'd suggest working through a few simple examples, say width = 16, n=15,16,17. Remember that & = bitwise AND, ~ = bitwise complement, and to use binary representation exclusively as you work through the examples.
It rounds n up to the next 'width' - but I think width needs to be a power of 2.
For example width == 8, n = 5:
(5 + 8 - 1) & ~(7)
= 12 & ~7
= 8
So 5 rounds to 8. Anything 1 - 8 rounds to 8. 9 to 16 rounds to 16. Etc. (0 rounds to 0)
It defines a macro called ROUNDUP which takes two parameters, n and width, and returns the value (n + width - 1) & ~unsigned(width - 1).
:)
Try this if you think you know what it does:
std::string s("WTF");
std::complex<double> c(-11,5);
ROUNDUP(s, c);
It won't work in C because of the unsigned. Here is what is does, as long as width is confined to powers of 2:
n width ROUNDUP(n,width)
----------------
0 4 0
1 4 4
2 4 4
3 4 4
4 4 4
5 4 8
6 4 8
7 4 8
8 4 8
9 4 12
10 4 12
11 4 12
12 4 12
13 4 16
14 4 16
15 4 16
16 4 16
17 4 20
18 4 20
19 4 20