how to calculate 8/16 bits ....

This is a discussion on how to calculate 8/16 bits .... within the C Programming forums, part of the General Programming Boards category; Hi, Could someone teach me how to calculate the range of 8-bits and 16-bits 2's complement numbers in binary, decimal ...

  1. #1
    lonewolf
    Guest

    how to calculate 8/16 bits ....

    Hi,
    Could someone teach me how to calculate the range of 8-bits and 16-bits 2's complement numbers in binary, decimal and hex.

    thanks

  2. #2
    Registered User alex's Avatar
    Join Date
    Sep 2001
    Posts
    132
    Hi!

    Let me first explain the (unsigned) binary system of counting by just counting. Let's take a 8 bit integer (an "unsigned char"):
    00000000 0
    00000001 1
    00000010 2
    00000011 3
    00000100 4
    00000101 5
    00000110 6
    00000111 7
    00001000 8
    and so on...
    01111111 127
    10000000 128
    and so forth...
    11111110 254
    11111111 255

    and in particular (a^b means "a to the power b" here, not the C operator ^):
    00000001 1 = 2^0
    00000010 2 = 2^1
    00000100 4 = 2^2
    00001000 8 = 2^3
    00010000 16 = 2^4
    00100000 32 = 2^5
    01000000 64 = 2^6
    10000000 128 = 2^7

    Signed integers behave a "sign bit". Most CPU's use the 2's complement variant. This is because it leaves addition and subtraction exacly the same operation. The most significant bit (leftmost in the usual notation) is the sign bit: 0 for positive values and 1 for negative values.

    00000000 0
    00000001 1
    00000010 2
    00000011 3
    00000100 4
    00000101 5
    00000110 6
    00000111 7
    00001000 8
    and so on...
    01111111 127
    10000000 -128
    10000001 -127
    and so forth...
    11111110 -2
    11111111 -1

    From this example, we can see that the smallest number is -2^7 (10000000 binary, 80 hex) and the largest number is (2^7)-1 (01111111 binary, 7f hex). For 16 bit integers the range is -2^15 (1000000000000000 binary, 8000 hex) to (2^15)-1 (0111111111111111 binary, 7fff hex).
    For a n-bit two's complement number, the range is given by -2^(n-1) to (2^(n-1))-1.

    This should help...
    alex

  3. #3
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    Just to add a little bit to alex's nice explanation:
    The easiest way to make a binary number its negative version is to flip all the digits (1 -> 0 and 0 -> 1) and just add one.

    00010100 = 20

    :flip:

    11101011
    + 1
    -------------
    11101100 = -20

    edit/addendum 1:

    Also, to go from binary to hex, chop the number into groups of 4.
    For example:

    11010100 = 212 (base 10)

    :split it:

    1101 = 13 = D
    0100 = 4 = 4

    So, D4 = 212

    For octal, do the same thing but split it into groups of 3.

    edit/addendum 2:

    Looking at the actual question, I just realized alex answered it perfectly and I just went nowhere fast Oh well, I'll just leave this up in case anyone was having trouble converting binary to hex or octal

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    And how do we print the binary of say, a number or a char, or a string? %b? hehe
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    something like this seba...
    Code:
    #include<stdio.h>
    
    
    
    void printbits(unsigned int tobeprinted)
    {
    unsigned int shift=8*sizeof(unsigned int)-1; // bits are 0-31 not 1-32
    unsigned int mask=1<<shift;
    printf("%i is ",tobeprinted);
    for(unsigned int i=1;i<=(shift+1);i++)
    {
    
    if(tobeprinted &mask) printf("1");
    else
    printf("0");
    tobeprinted <<= 1;
    if (i%8==0) printf(" ");
    }
    }
    
    int main()
    {
    for (int i=1;i<10;i++)
    {
    printf("Enter positive integer :-");
    unsigned int input;
    scanf("%i",&input);
    printbits(input);
    printf("\n");
    }
    return 0;
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    zoo
    Guest
    Originally posted by Sebastiani
    And how do we print the binary of say, a number or a char, or a string? %b? hehe
    Here's the lazy programmer's method.
    Code:
       int num = 32767;
       char str[33];
       itoa(num,str,2);
       printf("number in binary:%s\n",str);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SDLKey to ASCII without unicode support?
    By zacs7 in forum Game Programming
    Replies: 6
    Last Post: 10-07-2007, 04:03 AM
  2. Help counting number of bits set in an integer
    By JayDiddums10 in forum C Programming
    Replies: 5
    Last Post: 12-07-2006, 03:21 PM
  3. Writing binary data to a file (bits).
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 04:53 PM
  4. Small program that has to calculate miles per gallon
    By Guti14 in forum C++ Programming
    Replies: 6
    Last Post: 01-06-2004, 02:47 PM
  5. New idea on conveting byte to bits/bits to byte
    By megablue in forum C Programming
    Replies: 10
    Last Post: 10-26-2003, 01:16 AM

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