Type conversion: signed int to unsigned long in C

SpruceMoose

I'm currently up to chapter 2 in The C Programming Language (K&R) and reading about bitwise operations.

This is the example that sparked my curiosity:

x = x & ~077

Assuming a 16-bit word length and 32-bit long type, what I think would happen is 077 would first be converted to:

0000 0000 0011 1111 (16 bit signed int).

This would then be complemented to:

1111 1111 1100 0000.

My question is what would happen next for the different possible types of x? If x is a signed int the answer is trivial. But, if x is a signed long I'm assuming ~077 would become:

1111 1111 1111 1111 1111 1111 1100 0000

following 2s complement to preserve the sign. Is this correct?

Also, if x is an unsigned long will ~077 become:

0000 0000 0000 0000 1111 1111 1100 0000

Or, will ~077 be converted to a signed long first:

1111 1111 1111 1111 1111 1111 1100 0000

...after which it is converted to an unsigned long (no change to bits)?

Any help would help me clarify whether or not this operation will always set only the last 6 bits to zero.

0605002

Whatever data-type you choose, ~077 will set the rightmost 6 bits to 0 and all others to 1.

Assuming 16-bit ints and 32-bit longs, there are 4 cases:

Case 1

unsigned int x = 077; // x = 0000 0000 0011 1111
x = ~x; // x = 1111 1111 1100 0000
unsigned long y = ~x; // y = 0000 0000 0000 0000 1111 1111 1100 0000

Case 2

unsigned int x = 077; // x = 0000 0000 0011 1111
x = ~x; // x = 1111 1111 1100 0000
long y = ~x; // y = 0000 0000 0000 0000 1111 1111 1100 0000

Case 3

int x = 077; // x = 0000 0000 0011 1111
x = ~x; // x = 1111 1111 1100 0000
unsigned long y = ~x; // y = 1111 1111 1111 1111 1111 1111 1100 0000

Case 4

int x = 077; // x = 0000 0000 0011 1111
x = ~x; // x = 1111 1111 1100 0000
long y = ~x; // y = 1111 1111 1111 1111 1111 1111 1100 0000

See code here. This means the sign extension is done when the source is signed. When the source is unsigned, sign bit is not extended and the left bits are set to 0.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Type conversion - unsigned to signed int/char

From Dev

make_signed<unsigned long>::type is int?

From Dev

C++ signed and unsigned int vs long long speed

From Dev

C++ vector::size_type: signed vs unsigned; int vs. long

From Dev

Signed to unsigned conversion in C++

From Dev

Casting an unsigned long long int to signed long long int Is it possible?

From Dev

What is happening with signed/unsigned int conversion?

From Dev

signed or unsigned int in c++

From Dev

signed or unsigned int in c++

From Dev

Is conversion int -> unsigned long long defined by the standard

From Dev

C++ Implicit Conversion (Signed + Unsigned)

From Dev

Addition of unsigned long and int in C

From Dev

Why do bit fields in C need to be defined of type unsigned int or signed int

From Dev

narrowing conversion from int to long unsigned int {} is ill-formed in C++11

From Dev

Cast signed char to unsigned int in C

From Dev

Cast signed char to unsigned int in C

From Dev

Why can't I convert signed int to unsigned long

From Dev

Is a type cast necessary while converting between signed int and unsigned int?

From Dev

Is a type cast necessary while converting between signed int and unsigned int?

From Java

Objective-C implicit conversion loses integer precision 'NSUInteger' (aka 'unsigned long') to 'int' warning

From Dev

Cant solve "warning: conversion to 'long unsigned int' from 'int'"

From Dev

Adding signed and unsigned int

From Dev

Range of Unsigned & Signed int

From Dev

Narrowing conversion from 'long' to signed type 'char' is implementation-defined (strtol function in C)

From Java

c++ safeness of code with implicit conversion between signed and unsigned

From Java

C arithmetic conversion multiplying unsigned with signed and result in float

From Dev

C++ signed integer conversion to unsigned with more bits

From Dev

C# signed and unsigned integers are Narrowing conversion both ways?

From Dev

Implicit conversion loses integer precision: 'unsigned long' to 'int' - Error

Related Related

  1. 1

    Type conversion - unsigned to signed int/char

  2. 2

    make_signed<unsigned long>::type is int?

  3. 3

    C++ signed and unsigned int vs long long speed

  4. 4

    C++ vector::size_type: signed vs unsigned; int vs. long

  5. 5

    Signed to unsigned conversion in C++

  6. 6

    Casting an unsigned long long int to signed long long int Is it possible?

  7. 7

    What is happening with signed/unsigned int conversion?

  8. 8

    signed or unsigned int in c++

  9. 9

    signed or unsigned int in c++

  10. 10

    Is conversion int -> unsigned long long defined by the standard

  11. 11

    C++ Implicit Conversion (Signed + Unsigned)

  12. 12

    Addition of unsigned long and int in C

  13. 13

    Why do bit fields in C need to be defined of type unsigned int or signed int

  14. 14

    narrowing conversion from int to long unsigned int {} is ill-formed in C++11

  15. 15

    Cast signed char to unsigned int in C

  16. 16

    Cast signed char to unsigned int in C

  17. 17

    Why can't I convert signed int to unsigned long

  18. 18

    Is a type cast necessary while converting between signed int and unsigned int?

  19. 19

    Is a type cast necessary while converting between signed int and unsigned int?

  20. 20

    Objective-C implicit conversion loses integer precision 'NSUInteger' (aka 'unsigned long') to 'int' warning

  21. 21

    Cant solve "warning: conversion to 'long unsigned int' from 'int'"

  22. 22

    Adding signed and unsigned int

  23. 23

    Range of Unsigned & Signed int

  24. 24

    Narrowing conversion from 'long' to signed type 'char' is implementation-defined (strtol function in C)

  25. 25

    c++ safeness of code with implicit conversion between signed and unsigned

  26. 26

    C arithmetic conversion multiplying unsigned with signed and result in float

  27. 27

    C++ signed integer conversion to unsigned with more bits

  28. 28

    C# signed and unsigned integers are Narrowing conversion both ways?

  29. 29

    Implicit conversion loses integer precision: 'unsigned long' to 'int' - Error

HotTag

Archive