What Is Char I=0x80 And Why Overflow Did Not Happen In Bit Shifting

Just browsing Stack Overflow? Help us improve your experience. Sign up for research
    1. Home
    2. Questions
    3. Tags
    4. Users
    5. Companies
    6. Labs
    7. Jobs
    8. Discussions
    9. Collectives
    10. Communities for your favorite technologies. Explore all Collectives

  1. Teams

    Ask questions, find answers and collaborate at work with Stack Overflow for Teams.

    Try Teams for free Explore Teams
  2. Teams
  3. Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Get early access and see previews of new features.

Learn more about Labs what is char i=0x80 and why overflow did not happen in bit shifting Ask Question Asked 13 years, 5 months ago Modified 4 years, 3 months ago Viewed 19k times 8

Here is a program

#include <stdio.h> main() { unsigned char i=0x80; printf("i=%d",i<<1); }

The output it is giving is 256. I am not clear with what does

unsigned char i=0x80; <-- i is not int it is char so what will it store?

I know bitshift and hexadecimal things. How is the value of i being stored and how does it gets changed to 256?

UPDATE

Why did overflow not occurred when the bit shift operation happened?

Share Improve this question Follow edited Jun 18, 2011 at 19:12 Registered User asked Jun 18, 2011 at 18:52 Registered User's user avatar Registered UserRegistered User 5,34118 gold badges48 silver badges73 bronze badges 8
  • 3 Google "hexidecimal" and "bitshift operators." – Chris Lutz Commented Jun 18, 2011 at 18:54
  • I know bitshift and hexadecimal but I am not able to understand what will unsigned char i store as it is not an int – Registered User Commented Jun 18, 2011 at 18:55
  • @Registered User - char is an integral type. In C, character literals have type int. How do you know bitshift but not know that basic fact about C? – Chris Lutz Commented Jun 18, 2011 at 18:59
  • 1 You are correct that 256 "overflows" an unsigned character (on x86 at least) -- the "magic" is you are not passing a "unsigned char" value ;-) Compare with printf(..., (unsigned char)(128 << 1))user166390 Commented Jun 18, 2011 at 19:00
  • 1 @Registered - When you do i << 1 that operation is performed on an int, which also matches the "%d" in the format string. – Bo Persson Commented Jun 18, 2011 at 19:21
| Show 3 more comments

6 Answers 6

Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 20

In C, a char is an integer type used to store character data, typically 1 byte.

The value stored in i is 0x80 a hexidecimal constant that is equal to 128.

An arithmetic operation on two integer types (such as i << 1) will promote to the wider type, in this case to int, since 1 is an int constant. In any case, integer function arguments are promoted to int.

Then you send the result to printf, with a %d format specifier, which mean "print an integer".

Share Improve this answer Follow answered Jun 18, 2011 at 19:01 sverre's user avatar sverresverre 6,8892 gold badges28 silver badges35 bronze badges 0 Add a comment | 4

I think that K&R have the best answer to this question:

2.7 Type Conversions When an operator has operands of different types, they are converted to a common type according to a small number of rules. In general, the only automatic conversions are those that convert a narrower'' operand into awider'' one without losing information, such as converting an integer into floating point in an expression like f + i. Expressions that don't make sense, like using a float as a subscript, are disallowed. Expressions that might lose information, like assigning a longer integer type to a shorter, or a floating-point type to an integer, may draw a warning, but they are not illegal. A char is just a small integer, so chars may be freely used in arithmetic expressions.

So i<<1 converts i to int before it is shifted. Ken Vanerlinde has it right.

Share Improve this answer Follow answered Jun 18, 2011 at 19:24 grok12's user avatar grok12grok12 3,6466 gold badges27 silver badges28 bronze badges 0 Add a comment | 1

0x80 is hexadecimal for 128. The operation x << 1 means to left shift by one which effectively multiplies the number by two and thus the result is 256.

Share Improve this answer Follow answered Jun 18, 2011 at 18:56 Morten Kristensen's user avatar Morten KristensenMorten Kristensen 7,5734 gold badges33 silver badges52 bronze badges 14
  • @netrom all that I understand but the variable i is not integer it is a char so that is what I am not getting. – Registered User Commented Jun 18, 2011 at 18:56
  • Also, since printf is variadic, the result is cast to an unsigned int rather than back down to unsigned char which would truncate the value. – Chris Lutz Commented Jun 18, 2011 at 18:57
  • 5 The one thing missing in this answer is that the x is coerced to an int before the bitshift, so the result is also an integer, not an unsigned char. I think this is the biggest confusion presented in the question – Ken Wayne VanderLinde Commented Jun 18, 2011 at 19:03
  • 1 @Reg: As Ken said, x is coerced from unsigned char to unsigned int right before the left shift. That is why no overflow occurred. – Morten Kristensen Commented Jun 18, 2011 at 19:24
  • 1 @reg you need to get rid of the misconception that the computer stores values in decimal or hexadecimal. It doesn't. It uses binary. These binary values can be represented as decimal or hex or character but to the computer they are just integer values stored as binary. – David Heffernan Commented Jun 18, 2011 at 19:41
| Show 9 more comments 0

i=0x80 stores the hex value 0x80 in i. 0x80 == 128.

When printing out the value in the printf() format statement, the value passed to the printf() statement is i<<1.

The << operator is the unary bitwise shift left operator, which moves the bits in i to the left one position.

128 in binary is `10000000', shifting that to the right one bit gives '100000000' or 256.

Share Improve this answer Follow edited Jun 18, 2011 at 20:51 answered Jun 18, 2011 at 18:58 Chad's user avatar ChadChad 19k4 gold badges48 silver badges63 bronze badges 7
  • Based on some previous comments, it appears there is some confusion in what is actually stored in an unsigned character value. An unsigned character is simply an unsigned integral value of 8 bits. You may be familiar with this holding ASCII characters (like 'a', or 'V'), but these are still stored as 8-bit integral values. – Chad Commented Jun 18, 2011 at 19:00
  • @Chad ok you mean to say in this case 0x80 is treated as a hexadecimal number and then converted to int i.e. 128 why does it not store the character whose ASCII value is 128 – Registered User Commented Jun 18, 2011 at 19:00
  • I have an upvote, but it would be nice if this discussed the type of evaluating unsigned_char << 1 to round it out.user166390 Commented Jun 18, 2011 at 19:01
  • 2 Err, 128 in binary is 10000000, shifting by 1 overflows the bounds of unsigned char on most platforms but due to printf being variadic it isn't cast back down. – Chris Lutz Commented Jun 18, 2011 at 19:02
  • The character whose ASCII value is 128 (€) is always stored as 0x80, in fact they are equivalent. Try a simple program that checks if '€' == 0x80 – Chad Commented Jun 18, 2011 at 19:09
| Show 2 more comments 0

i << 1 isn't being stored in i. Thus, there's no question of overflow and 256 is the output.

The following code will give 0 as the output.

#include <stdio.h> main() { unsigned char i=0x80; i = i<<1; printf("i=%d",i); } Share Improve this answer Follow answered Jun 13, 2014 at 15:04 NirAv JaIn's user avatar NirAv JaInNirAv JaIn 1,0551 gold badge9 silver badges16 bronze badges Add a comment | 0

0x80 is a hexadecimal constant Which is equivalent to 128. And << is a left shift operator Which says that if x<<y Then x * (2^y). So 128 * 2^1 (here y=1, the shifting value) = 256.

Share Improve this answer Follow answered Aug 12, 2020 at 15:57 apoorva sharma's user avatar apoorva sharmaapoorva sharma 1 Add a comment |

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid …

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Draft saved Draft discarded

Sign up or log in

Sign up using Google Sign up using Email and Password Submit

Post as a guest

Name Email

Required, but never shown

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.

  • The Overflow Blog
  • Your docs are your infrastructure
  • Featured on Meta
  • More network sites to see advertising test [updated with phase 2]
  • We’re (finally!) going to the cloud!
  • Call for testers for an early access release of a Stack Overflow extension...

Linked

-1 How can 256 be represented by a char? 14 Why does (1 >> 0x80000000) == 1? 5 Cannot understand shift operator behavior in C code 2 Need clarification on bit shifting 1 bit shifting in C, unexpected result 0 bit shift in c language 34 Why does ((unsigned char)0x80) << 24 get sign extended to 0xFFFFFFFF80000000 (64-bit)? 2 Unexpected results while bit-shifting in C 0 c and bit shifting in a char 3 Why does shifting a variable by more than its width in bits zeroes out? 3 Importance of '0' character and << operator in this program

Hot Network Questions

  • What do border officials do with my passport when I tell them that I'm entering as the spouse of an EU national?
  • Can mantras in Buddhist meditation be recited in translation, or does it need to be recited in Sanskrit?
  • Polynomial.java - a Java class for dealing with polynomials with BigDecimal coefficients
  • testing for a correlation between a real number and percentage accuracy
  • Weapon Mastery and Weapon Cantrips
  • How to design for API use cases that need different data from the same table?
  • Is there a symbol for the Hyper key?
  • Does length contraction "break the speed limit"?
  • Is Holy Terra Earth?
  • Counting birds outside my house
  • Is there any advantage to using gifts to avoid estate tax?
  • What adaptations are necessary to make a horse-sized beetle possible?
  • What is small arch between two notes and how to play it?
  • Groups with no proper non-trivial fully invariant subgroup
  • The British used to (still?) classify their guns by weight in pounds rather than caliber. Was that a constant across shell types?
  • Can you please advise on kerning?
  • Problem with highlighting first row and column of a table
  • What is this insect found in an apartment in Brazil?
  • Find the Smallest Data Type for a Number
  • Publishing corollaries of previously published results
  • If scent means a pleasant smell, why do we say "lovely scent" or "sweet scent"?
  • What would T-Rex cavalry be used for?
  • How to attribute authorship to personal non-academic friend who made significant contributions
  • Why sand dunes appear dark in Sentinel-1 SAR Images but bright in optical images
more hot questions Question feed Subscribe to RSS Question feed

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

lang-c

Từ khóa » C 0x80