Thread: Shifting an unsigned char

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    54

    Shifting an unsigned char

    In the following program I am trying to left shift a char variable.

    Code:
    int main()
    {
        unsigned char c='0x0A';
        printf("%c", (c << 4));
        
        return 0;
    }

    When I compile and run the program then it is giving garbage value. My requirement is 0xA0. Any suggestions on this.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    Code:
    #include <stdio.h>
     
    int main() {
        unsigned char c = '\x0A'; // proper way to set to hex value
        printf("%X\n", c << 4);   // print as hex integer
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    0xA0 isn't ASCII or a valid UTF-8 char. In ISO-8859-1 (Latin1) or WINDOWS-1252 is a "non breaking space"...
    The printed char depends on your encoding... Example... if you do:

    Code:
    puts("\xe2\x98\xba");
    You'll see a smiley in UTF-8 or "â~º" in WINDOWS-1252.

    Quote Originally Posted by krkr View Post
    In the following program I am trying to left shift a char variable.

    Code:
    int main()
    {
        unsigned char c='0x0A';
        printf("%c", (c << 4));
        
        return 0;
    }

    When I compile and run the program then it is giving garbage value. My requirement is 0xA0. Any suggestions on this.

  4. #4
    Registered User
    Join Date
    Feb 2014
    Posts
    54
    What is the difference between storing '0xAA' and '\xAA' in char.
    My understanding about '0xAA' is, the ASCII value will be stored in memory. But my doubt is, why it prints only 41 during printf, which is ASCII value of 'A' not 'AA'.
    Not sure what will be stored in memory when we use '\xAA'.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by krkr
    What is the difference between storing '0xAA' and '\xAA' in char.
    It seems to me that '0xAA' is a multi-character constant formed from the characters '0', 'x', 'A', and 'A', whereas '\xAA' is a character constant having a value of 0xAA.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by laserlight View Post
    It seems to me that '0xAA' is a multi-character constant formed from the characters '0', 'x', 'A', and 'A', whereas '\xAA' is a character constant having a value of 0xAA.
    Wow... I didn't notice the '0xAA' (between single quotes)...

  7. #7
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    If your requirement is 0xA0 after the shift then perhaps you should be writing unsigned char c = 0x0A; // (no single quotes etc necessary)

  8. #8
    Registered User
    Join Date
    Feb 2014
    Posts
    54
    Quote Originally Posted by Hodor View Post
    If your requirement is 0xA0 after the shift then perhaps you should be writing unsigned char c = 0x0A; // (no single quotes etc necessary)
    I got a doubt, what will happen if we assign a char variable without single quotes. So replaced c = '0x0A' with c=0x0A and printed using %c, I am getting junk value "▒" always. Tried replacing 0x0A with 0xAA, 0xBB etc. But same junk value "▒" is coming always. How it will store in memory if we assign without single quotes in a char variable.

    If we print using %X then it is coming as 0xA0.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by krkr
    So replaced c = '0x0A' with c=0x0A and printed using %c
    The problem is that you're using the %c format specifier to print a non-printable character in this context. Try: c=0x4B or c=0x7A

    Quote Originally Posted by krkr
    If we print using %X then it is coming as 0xA0.
    So you know that it is correct. You don't have to worry about the strange output you get when using %c instead. The value is not "junk"; it's the output that is "junk" because you're trying to print what is non-printable.
    Last edited by laserlight; 01-20-2020 at 11:24 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How covert unsigned char[4] to unsigned integer?
    By barracuda in forum C Programming
    Replies: 110
    Last Post: 02-23-2015, 04:00 AM
  2. Unsigned char array to unsigned char?
    By Kishintai in forum C Programming
    Replies: 16
    Last Post: 04-26-2013, 01:37 PM
  3. Replies: 4
    Last Post: 07-24-2012, 10:41 AM
  4. Replies: 2
    Last Post: 10-06-2009, 09:37 AM
  5. unsigned char vs signed char and range of values
    By Silvercord in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2003, 01:30 PM

Tags for this Thread