Thread: Shift Function

  1. #1
    Registered User
    Join Date
    Sep 2008

    Shift Function

    I do not think I am using the "shift" correctly in my program ? I am trying to cycle the bytes so I would get the value of the bit pattern once I shift the bytes. something to the effect :

    01 00 00 00 = 1

    once it hit FF it would "shift" the bytes so I would get :

    00 01 00 00 = 256
    00 02 00 00 = 512

    and so on ...................

    I am thinking about adding a function and not trying to do it all in the main ? Anyone have any ideas ? Thank you for the help!

    #include <stdio.h>
    int main()
     int i;
     for (i =0; i< 256; i++)
     printf( "The Value is:   %02X 00 00 00     %14d     %14u\n", i,i,i);
     if ( i == 255)
     i = i >> 2;
     for (i =0; i< 256; i++)
     printf( "The Value is:  00 %02X 00 00      %14d     %14u\n", i,i,i);
     if ( i == 255)
     i = i >> 2;
      for (i =0; i< 256; i++)
     printf( "The Value is:  00 00 %02X 00      %14d     %14u\n", i,i,i); 
     if ( i == 255)
     i = i >> 2;
      for (i =0; i< 256; i++)
     printf( "The Value is:  00 00 00 %02X       %14d     %14u\n", i,i,i); 
    return (0);

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    for ( i = 0 ; i < 0x100 ; i++ )

    for ( ; i < 0x10000 ; i += 0x100 )

    for ( i < 0x1000000 ; i += 0x10000 )

    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    No 'if' statements are required. When the loops exit the number is equal to the number in the less-than comparison, i.e. 256.

    Instead of this "%02X 00 00 00" You'll want something like this: "%08X"

    You can remove the second getch. No code ever executes after a return.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Join Date
    Oct 2007
    Inside my computer
    You're re-initializing i to 0 in every loop.
    And one space for indentation is just not sufficient.
    Plus it's messed up, because for every indentation level, there should be more spaces, not less.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM