Shift Function

This is a discussion on Shift Function within the C Programming forums, part of the General Programming Boards category; I do not think I am using the "shift" correctly in my program ? I am trying to cycle the ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    22

    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!

    Code:
    #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); 
     
     
    getch();
    return (0);
    getch();
    }

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

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

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

    etc
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    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
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,988
    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, 09:18 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  3. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  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, 07:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21