bitwise operators?

This is a discussion on bitwise operators? within the C++ Programming forums, part of the General Programming Boards category; >>The article uses a very complex vocabulary system Howstuffworks has a very simple article on binary representation of numbers, it's ...

  1. #16
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    >>The article uses a very complex vocabulary system

    Howstuffworks has a very simple article on binary representation of numbers,
    it's not as long as it looks, [sarcasm]they were very clever and printed the first
    127 ASCII characters vertically [/sarcasm]

    Also see this for an intro to converting between binary and decimal
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  2. #17
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Evil,

    Are you using that book that introduces bitwise operations in the 3rd chapter? (Horton or Schiltd???) I've forgotton what book that is, but that's nuts!!!! (It is something you will need to know eventually, but most C/C++ programs don't have any bitwise operations.)

    The only reason this is hard is because binary is not built-into cin or cout.

    The secret to binary/bitwise operations is: Hexadecimal.

    Hex is built into cin and cout.


    Hex is easier than decimal because, with a little practice you can learn to convert between hex and binary (any size numbers) in your head! It's one more thing to learn, but you only have to memorize the 16 conversions from 0 to F... and you already know 0 and 1.

    In my "hardware world", we work with individual bits every day and we never mix binary and decimal! I might say "I'm reading FE", I would not say "I'm reading 254".

    Binary -> Hex
    0000 -> 0
    0001 -> 1
    0010 -> 2
    0011 -> 3
    0100 -> 4
    0101 -> 5
    0110 -> 6
    0111 -> 7
    1000 -> 8
    1001 -> 9
    1010 -> A
    1011 -> B
    1100 -> C
    1101 -> D
    1110 -> E
    1111 -> F

    BTW - The calculator built-into Windows can do conversions between binary, octal, decimal, and hex, if you set the view to Scientific.


    Doug.

  3. #18
    Registered User
    Join Date
    Sep 2005
    Posts
    14
    I know this is probably a bump, after such a long time since originally being posted. I thought I posted a reply to this and posted it, I don't know what happened.

    Doug I understand how the book was doing this in binary now. The hexadecimal things seems easy enough to understand (Even if there is no source anywhere to learn it) so maybe I will learn it some other day.

    Thank you everyone so much for your help!

    An yes I know I am being a total lazy ass, I just cannot help it!

  4. #19
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114
    Maybe you could see it that way:

    decimal: 5432

    is like
    (5 * (10^3)) + (4 * (10^2)) + (3 * (10^1)) + (2 * (10^0))
    (5 * 1000) + (4 * 100) + (3 * 10) + (2 * 1)
    5000 + 400 + 30 + 2
    = 5432

    so you just take the digits positions and add them together by their digits, such
    as 5, being at the position 10^3, where the 10 represent the base, decimal, and 3, the
    thousands multiplication to 5...

    In all other bases, it works the same way, only the "10"s change for 16, 8, 2... depending on the current base.

    So for binary (base 2),
    1011
    is like
    (1 * (2^3)) + (0 * (2^2)) + (1 * (2^1)) + (1 * (2^0))
    (1 * 8) + (0 * 4) + (1 * 2) + (1 * 1)
    8 + 2 + 1
    = 11

    And for hexadecimal, (base 16) 0FA5
    is like
    (0 * (16^3)) + (F * (16^2)) + (A * (16^1)) + (5 * (16^0))
    (0 * 4096) + (F * 256) + (A * 16) + (5 * 1)
    (0 * 4096) + (15 * 256) + (10 * 16) + (5 * 1)
    3840 + 160 + 5
    = 4005

    You have to know that 0 1 2 3 4 5 6 7 8 9 A B C D E F
    Is equivalent in decimal to 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

    You should be able to get something out of this...

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise operators
    By gnewfenix in forum C Programming
    Replies: 2
    Last Post: 05-16-2009, 08:43 PM
  2. Bitwise Operators
    By rrc55 in forum C Programming
    Replies: 6
    Last Post: 04-30-2009, 11:37 AM
  3. Palindromes and Bitwise operators
    By Dr Tornillo in forum C Programming
    Replies: 8
    Last Post: 08-02-2007, 02:31 PM
  4. bitwise and arithmetic Operators
    By Whiteghost in forum C Programming
    Replies: 4
    Last Post: 12-28-2006, 01:13 PM
  5. Bitwise Operators, Help!!
    By Mini__C in forum C Programming
    Replies: 6
    Last Post: 07-14-2004, 04:20 PM

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