![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 | |||
|
Hmmmm ... Is there more??
Join Date: Apr 2008
Location: Post Falls, ID
Posts: 15
Rep Power: 0
![]() |
Re: Bitwise Operators...
Quote:
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 = 0The 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 0000Quote:
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:
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 |
|||
|
|
|
|
|
#12 | |||
|
Caffeinated Neural Net
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 889
Rep Power: 4
![]() |
Re: Bitwise Operators...
Quote:
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) 0001 0002 0004 0008 0010 0020 0040 0080 0100 0200 0400 #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 #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 #define BIT_31 0x80000000 #define BIT_31 2147483648 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:
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:
__________________
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 |
|||
|
|
|
|
|
#13 |
|
Newbie
Join Date: Apr 2008
Posts: 10
Rep Power: 0
![]() |
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)
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |