mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 09:41:36 +00:00
c.html: fix #1021 (bitwise negation and shifting into the sign bit)
0x0f is of type int, for 32-bit int the result is 0xfffffff0. 31'st bit is the sign bit of a 32-bit wide int.
This commit is contained in:
parent
1f49ae9306
commit
e267eed62c
@ -234,7 +234,7 @@ int main() {
|
|||||||
// same with j-- and --j
|
// same with j-- and --j
|
||||||
|
|
||||||
// Bitwise operators!
|
// Bitwise operators!
|
||||||
~0x0F; // => 0xF0 (bitwise negation, "1's complement")
|
~0x0F; // => 0xFFFFFFF0 (bitwise negation, "1's complement", example result for 32-bit int)
|
||||||
0x0F & 0xF0; // => 0x00 (bitwise AND)
|
0x0F & 0xF0; // => 0x00 (bitwise AND)
|
||||||
0x0F | 0xF0; // => 0xFF (bitwise OR)
|
0x0F | 0xF0; // => 0xFF (bitwise OR)
|
||||||
0x04 ^ 0x0F; // => 0x0B (bitwise XOR)
|
0x04 ^ 0x0F; // => 0x0B (bitwise XOR)
|
||||||
@ -242,7 +242,7 @@ int main() {
|
|||||||
0x02 >> 1; // => 0x01 (bitwise right shift (by 1))
|
0x02 >> 1; // => 0x01 (bitwise right shift (by 1))
|
||||||
|
|
||||||
// Be careful when shifting signed integers - the following are undefined:
|
// Be careful when shifting signed integers - the following are undefined:
|
||||||
// - shifting into the sign bit of a signed integer (int a = 1 << 32)
|
// - shifting into the sign bit of a signed integer (int a = 1 << 31)
|
||||||
// - left-shifting a negative number (int a = -1 << 2)
|
// - left-shifting a negative number (int a = -1 << 2)
|
||||||
// - shifting by an offset which is >= the width of the type of the LHS:
|
// - shifting by an offset which is >= the width of the type of the LHS:
|
||||||
// int a = 1 << 32; // UB if int is 32 bits wide
|
// int a = 1 << 32; // UB if int is 32 bits wide
|
||||||
|
Loading…
Reference in New Issue
Block a user