Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 28th, 2008, 1:05 PM   #11
opa6x57
Hmmmm ... Is there more??
 
opa6x57's Avatar
 
Join Date: Apr 2008
Location: Post Falls, ID
Posts: 15
Rep Power: 0 opa6x57 is on a distinguished road
Re: Bitwise Operators...

Quote:
Originally Posted by redfiretruck View Post
Yes. Open your windows calculator - and switch to 'scientific view'. Then, there are 4 radio buttons in the upper left portion of the calculator's keyboard. You can quickly see how some of these numbers work if you type in a number (say 8), then click the 'bin' radio button - the calculator will convert the 8 into it's binary equivalent - 1000.

This calculator also has some of the logical operators you're trying to understand ... there's an 'AND' and an 'OR' you can play with these in either BINARY mode or in DECIMAL mode. In fact, you can switch between the modes - as you go through some of the steps.

In Decimal, for example ...
255 {AND} 5 = 5 
250 {AND} 5 = 0

The binary equivalent of this would be:
255 = 1111 1111
5   = 0000 0101
1111 1111 {AND} 0000 0101 = 0000 0101
1111 1010 {AND} 0000 0101 = 0000 0000


Quote:
Originally Posted by redfiretruck View Post
  • I have read that chars are representated by integers correct? Such as Char 'A' is 65? Well, how does the computer recognize the difference between 65 and A? Just wondering ^_^.
This is really determined by the datatype of the variable used. (I'm most familar with the BASIC forms of this ... so, I should probably let some of the C programmers answer your question about this in C.)

In most forms of BASIC - you would tell by how you dimension the variable.
If I wanted to use the ASCII codes (the '65' in your example) then I'd have to dimension the variable as a byte or integer. If, I wanted to use the variable to hold the letter "A", then I'd have to dimension the variable as a string of some kind.


Quote:
Originally Posted by redfiretruck View Post
  • Also... When i read through stuff like source codes and the example listed above that is in code... Even with comments, they are confusing and take a few minutes to understand? Is that normal for someone starting out?
Yes.
Think about your favorite book. Now, imagine someone gave you a copy of that book which had been translated into Russian. You'd have a very difficult time reading your favorite book - since it's written in a language you don't understand.

Learning a new programming language is probably not as bad as Russian - but, it could be...

(I apologize for posting my example code in VB syntax - I used what I'm most familar with... eeeek.)
__________________
Ken -
New to PFO ... but been dabbling in various versions of BASIC since highschool - circa 1973.

"Shouldn't the 'Air and Space' museum be empty?" - Dennis Miller
opa6x57 is offline   Reply With Quote
Old Apr 28th, 2008, 3:16 PM   #12
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 889
Rep Power: 4 lectricpharaoh will become famous soon enough
Re: Bitwise Operators...

Quote:
Originally Posted by redfiretruck
Is this a correct representation of how bits work? Based on what your all saying, it is generally true... (I mean how you read bits)... Link: http://l3d.cs.colorado.edu/courses/C...96/binary.html
I glanced at that link, and it seems correct. However, there's something I'd like to add. C does not have support for expressing numbers (in your source) as binary. You can pick decimal (the default), octal (base 8), and hexadecimal (base 16). Hexadecimal is particularly useful.

First, if you have a four-digit binary number, it can store 2 to the power of 4 (generally written as 2^4 on forums and such, when we can't actually use a superscript for the exponent). This means it can store 16 different values. A hexadecimal digit, by its very nature, can also store one of 16 different values; we can thus represent four binary digits with one hex digit.

I mean, what's easier to type, of these two numbers?
11111110110111000100001100100001 (a 32-bit binary number)
FEDC4321 (the same value, expressed as hexadecimal)
Hexadecimal numbers are also preferable to decimal in some cases, especially when expressing powers of two. For example, here is a series of numbers, with each being double the preceding number:
0001
0002
0004
0008
0010
0020
0040
0080
0100
0200
0400
Note how they are all four digit numbers. I can represent any number from 0 to 65535 (or -32768 to -32767, if it's signed) with four hexadecimal digits, not counting the 0x prefix; this is less than the number of decimal digits I'd need. Things also line up nicer if I'm declaring constants in hexadecimal:
#define BIT_0 0x01
#define BIT_1 0x02
#define BIT_2 0x04
#define BIT_3 0x08
#define BIT_4 0x10
#define BIT_5 0x20
#define BIT_6 0x40
#define BIT_7 0x80
Contrast this with the same code using decimal:
#define BIT_0 1
#define BIT_1 2
#define BIT_2 4
#define BIT_3 8
#define BIT_4 16
#define BIT_5 32
#define BIT_6 64
#define BIT_7 128
Granted, the difference doesn't seem like a lot here, but this is for only eight bits. If you've got a 32-bit value, and each bit is used for a flag, you get code like:
#define BIT_31 0x80000000
instead of:
#define BIT_31 2147483648
You tell me which is easier to read.
In C, you will express these numbers with the prefix '0x' (that's a zero, not an uppercase 'o'). This is because hexadecimal numbers can have letters in them; the letters A through F are used to represent the values 10 through 15 in a single digit (since decimal doesn't have enough symbols, we needed to adopt some from the alphabet). If you didn't use the prefix, and had a number such as FE75, the compiler would consider this a variable name (since it doesn't start with a number). Make it 0xFE75, and the compiler knows it's a hex number.

For quick reference, just as the digits for decimal go 0, 1, 2, 3, 4, 5, 6, 7, 8, and finally 9, hexadecimal digits go 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and finally F. Note that although I write them in uppercase, most times it doesn't matter (C compilers don't care). I just find it clearer when the letters are uppercase.
Quote:
Originally Posted by redfiretruck
I have read that chars are representated by integers correct? Such as Char 'A' is 65? Well, how does the computer recognize the difference between 65 and A? Just wondering ^_^.
All values stored by your computer are binary numbers; as such, they are a series of one or more bits. A byte is defined as the smallest unit you can directly address (ie, you cannot directly read or write a single bit); on almost all modern systems, a byte is synonymous with an eight-bit value. Don't make the mistake of thinking a byte is always eight bits, though; if you want to be clear, either say 'eight-bit byte', or use the term 'octet' (meaning an eight-bit value).

Now, as for the computer knowing how to interpret a value? Well, that depends what you declared it as, and what functions you use. You will also note that some of the C standard functions that you might think would return a single char actually return an int instead; this is because an int can represent values that a char typically cannot, and these values can be used to signal an error condition. For example, assume that a valid character will be in the range of 0 to 255, and an error is signaled by 0xFFFF (65535 or -1, depending on whether the value is signed or not). Either way, the error return value is outside the range of valid non-error values.

Short answer: they're all numbers to the computer; their significance depends on how you use them.
Quote:
Originally Posted by redfiretruck
Also... When i read through stuff like source codes and the example listed above that is in code... Even with comments, they are confusing and take a few minutes to understand? Is that normal for someone starting out?
Yup. It's also a lot harder, in many cases, to read code written by someone else than to write your own. When you're writing your own, all you have to do is figure out how to express a problem to the compiler. When you're reading someone else's code, you not only have to figure out what they are doing, but why they are doing it; this is what makes it difficult. It's also why short and sweet examples that illustrate a language feature without unnecessary extras are valuable (I think many examples on MSDN fall short of this simplicity requirement). However, as you begin to make sense of code written by others, your skills will become that much stronger.
__________________
A man's knowledge is like an expanding sphere, the surface corresponding to the boundary between the known and the unknown. As the sphere grows, so does its surface; the more a man learns, the more he realizes how much he does not know. Hence, the most ignorant man thinks he knows it all. - L. Sprague de Camp
lectricpharaoh is offline   Reply With Quote
Old May 6th, 2008, 4:10 PM   #13
misho
Newbie
 
Join Date: Apr 2008
Posts: 10
Rep Power: 0 misho is on a distinguished road
Re: Bitwise Operators...

As a few more examples:

There are programs which use something called bitmasks to store certain boolean settings. A boolean uses 1 byte (8 bits) of space of which only 1 bit is really needed (well, in C89 there isn't even a boolean). It's fairly obvious that wasting 7/8 of the space you are using is generally a bad thing (although, on modern computers, it's really not a big deal). What you can do is use a byte (char) to store 8 boolean values. So, let's say that you have the following byte and you want 0 to signify false:

mask = 01001110; <- this is pseudocode, and the number would be decimal 78;

Let's say the boolean value you're interested in is in the 3rd least significant bit (01001 1 10), then you could access its value like so:

c Syntax (Toggle Plain Text)
  1. unsigned char mask = 78; //or whatever, in fact, this would just be declared like this; it would probably be declared to 0 and modified with other functions
  2.  
  3. if(mask & 1<<2) //this evaluates to 00000100 in our case, so true
  4. {
  5. //do something
  6. }
  7. else
  8. {
  9. //do something
  10. }

And to set the value of a specific bit (the 3rd least significant), you would go:

mask |= 1<<2;  // this makes it 1
mask &= ~(1<<2);  // this makes it 0

Another interesting example is a sorting algorithm called radix sort. This one is cool because typical sorting algorithms are O(n^2) or O(n \log n) (in case you don't know this notation, it tells you how you can expect your algorithm to perform. in the O(n^2) case, if you double the number of values you're sorting, the algorithm would take about 4 times longer because of the squared relationship). In general, you want the power of n to be as small as possible. Well, if you know something about the numbers you're sorting, you can do better than O(n \log n). With radix sort, we can do it in O(n) time (i.e. double the number of values to be sorted, the time to sort them doubles). Without getting too involved in this, if you know you have numbers between 0 and 999, you can sort numbers by the number in their units place first, then by the number in their tens place, then by the number in their hundreds place, and then they get sorted. In binary (radix is 2), you can do the same thing. So if you know you're sorting ints, then you can sort by each bit, and to get the value of each bit, you would use the method shown above. The only thing is that the negatives would come after the positives if you aren't careful (that's because of something called 2's complement).
misho is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Interesting Bitwise tricks Wizard1988 Coder's Corner Lounge 9 Nov 25th, 2007 7:09 PM
Bitwise Operator Question grimpirate PHP 1 Nov 13th, 2006 2:39 AM
Boolean operators?????? SpaderS C++ 4 Mar 20th, 2006 8:24 PM
bitwise operators and type double ionexchange C++ 14 Feb 12th, 2006 9:27 AM
need help with bitwise operations metsfan C 17 Feb 2nd, 2006 6:09 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:56 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC