% function help

This is a discussion on % function help within the C Programming forums, part of the General Programming Boards category; I have some c code I need help explaining what is happening with %: Code: string % 0xffff If 'string' ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    19

    % function help

    I have some c code I need help explaining what is happening with %:

    Code:
    string % 0xffff
    If 'string' is equal to 12345678, the outcome of the operation should be 0x68ac (or 26796 in decimal form).

    But what is actually happening at the % in simple terms? I need help understanding this because I have to implement it in one of my projects. Thanks

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    Well first you need to convert your string to an integer, say with strtol(), then % is the modulo operator (or remainder operator)
    eg.
    7 % 3 = 1
    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
    Registered User
    Join Date
    Aug 2006
    Posts
    19
    If I try: 12345678 % 0xffff

    is that the same as

    12345678 % 65535?

    If so, I am getting 25098 as a result when I should be getting 26796 (0x68ac).

    I am stupid when it comes to this -__-, sorry.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    > If so, I am getting 25098 as a result when I should be getting 26796 (0x68ac).
    25098 is the result I would expect.
    It's the result I get with windows calculator and
    Code:
    #include<stdio.h>
    int main(void) {
        int a = 12345678;
        printf( "%d\n", a % 0xffff );
        return 0;
    }
    What gives you the result you expect?
    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.

  5. #5
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    Yes 0xffff is the same as 65535, but what makes you think that you would get 26796 as a result? 25098 is correct.
    EDIT: too late

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    19
    You guys are correct, thank you, question answered here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

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