6 Replies - 634 Views - Last Post: 31 August 2009 - 06:09 PM Rate Topic: -----

#1 learnplaycreate  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 119
  • Joined: 27-August 09

how does this code work

Post icon  Posted 30 August 2009 - 10:25 PM

hey guys, i've found this code that after receiving a decimal input outputs the hex number, my question is how is the cout line working inside the loop, without the []following the "0123456789abcdef" it outputs the whole string 7 times, the section inside the [] appears to be allowing the program to choose which element of the string to output, if someone could please explain how it is choosing that would be really helpful. thanx guys
#include <iostream>
using namespace std;

int main ()
{

	int n;
	while (cin >> n) {
		cout << "decimal: " << n << endl;

		//--- Print hex with leading zeros
		cout << "hex	: ";
		for (int i=2*sizeof(int) - 1; i>=0; i--) {
			cout << "0123456789abcdef"[((n >> i*4) & 0xF)];
		}
		cout << endl << endl;
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: how does this code work

#2 Guest_c.user*


Reputation:

Re: how does this code work

Posted 30 August 2009 - 10:56 PM

if you will do cout << "abc"[0] << endl; it will print a because this string created in a static memory as a character array (which has an address of any element)

right shifts for signed values with negative sign are undefined

This post has been edited by c.user: 30 August 2009 - 10:57 PM

Was This Post Helpful? 0

#3 learnplaycreate  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 119
  • Joined: 27-August 09

Re: how does this code work

Posted 31 August 2009 - 03:07 AM

View Postc.user, on 30 Aug, 2009 - 09:56 PM, said:

if you will do cout << "abc"[0] << endl; it will print a because this string created in a static memory as a character array (which has an address of any element)

right shifts for signed values with negative sign are undefined

i have a basic understanding of character arrays and pointers, i don't understand is where the conversion is happening how is the reference of memory size achieving a conversion between dec and hex? or how does shifting n to the right facilitate this conversion?

what function does the 0xF have? i can comment it out and the function still works.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5800
  • View blog
  • Posts: 12,635
  • Joined: 16-October 07

Re: how does this code work

Posted 31 August 2009 - 04:13 AM

Here's the code with some comments:
void showHex(int n) {
	cout << "decimal: " << n << endl << "hex: ";
	
	// sizeof(int) == number of bytes in an int
	// sizeof(int)*2 == number of nibbles, or 4 bit values
	// 0xf == 1111b
	for (int i=2*sizeof(int) - 1; i>=0; i--) {
		// shift the value of n i*nibble size bits over
		int nibble = (n >> i*4); 
		nibble &= 0xF; // AND the bits so that only the last 4 bits are used
		// at this point, nibble is a value between 0 and 15
		// dynamic casting of a string, kind of ugly, but legal.
		cout << "0123456789abcdef"[nibble];
	}
	cout << endl << endl;
}



It's being intentionally cute, of course. Part of the issue is figuring out how to start working with bits from the front, rather than the back. If you really just want a string, though, this is not a problem.

Here's how I'd do it:
string dec2hex(int n) {
	string s = "";
	for (int i=0; i<sizeof(int)*2; i++) {
		char ch = n & 0x0f;
		ch += (ch<10) ? '0' : ('A'-10);
		s = ch + s;
		n >>= 4;
	}
	return s;
}


Was This Post Helpful? 0
  • +
  • -

#5 Guest_c.user*


Reputation:

Re: how does this code work

Posted 31 August 2009 - 04:15 PM

learnplaycreate said:

how is the reference of memory size achieving a conversion between dec and hex

it is happen by formula

0 - 0000
1 - 0001
2 - 0010
3 - 0011
4 - 0100
...
A - 1010
B - 1011
C - 1100
D - 1101
E - 1110
F - 1111
10 - 0001 0000
11 - 0001 0001
12 - 0001 0010
...
1F - 0001 1111
20 - 0010 0000
21 - 0010 0001
etc



it can be chunked to 0000 0000 for hex and 000 000 for oct
Was This Post Helpful? 0

#6 Guest_c.user*


Reputation:

Re: how does this code work

Posted 31 August 2009 - 04:21 PM

10 in hex and 16 in dec have equal binary representations
A in hex and 10 in dec are equal to 1010 bin

This post has been edited by c.user: 31 August 2009 - 04:22 PM

Was This Post Helpful? 0

#7 nathanpc  Icon User is offline

  • SCIENCE!
  • member icon

Reputation: 112
  • View blog
  • Posts: 1,171
  • Joined: 31-July 09

Re: how does this code work

Posted 31 August 2009 - 06:09 PM

First, you need to have in your mind the calc to convert decimals to hexamals, the second is only a comment, this code is a simple thing, but if you take a look inside it, you will see a vey complex thing.

Thanks!

And very good table of binary c.user!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1