Hello;
I am a newbie.
A homework assignment was assigned and I am having trouble getting started.
The assignment reads:
Write a program to print out the binary value of a 16 bit number.
Create integers i, count, and mask.
Set 'i' to a hex value of 0x1b53.
Set mask to a value of 0x8000. Why?
print a line to show the hex value of i and then the leader for the binary value like this:
Hex value = 1b53 Binary=
Use a for loop to loop 16 times and print 16 digits, using count as the loop counter
To test for each digit value, bitwise and 'i' with 'mask'
when the result for the bitwise and is true, print the number '1'
when the result for the bitwise and is false, print the number '0'
then shift mask one place to the right
print a new line and then quit
Use prtscrn and make a hard copy of the code with the console output.
Extra: use the modulus of count and print a space after every 4th digit to make the binary easier to read
The output should look like this:
Hex value = 1b53, Binary= 0001 1011 0101 0011
Any suggestions?
12 11231
Hex value = 1b53, Binary= 0001 1011 0101 0011
To see of this bit is set: -
0001 1011 0101 0011
-
.............^
-
which is bit 4 and represents a value of 16, divide the number 1b53 by 16 and check the reminder. If it is 0, the bit is off. If not the bit is on.
That's enough of a hint.
To see of this bit is set: -
0001 1011 0101 0011
-
.............^
-
which is bit 4 and represents a value of 16, divide the number 1b53 by 16 and check the reminder. If it is 0, the bit is off. If not the bit is on.
That's enough of a hint.
Well, maybe the hint is enough for a person familiar with programing...
I have never taken any programming classes. We are in our 4th week of C.
Well, thanks for the quick reply.
Well, maybe the hint is enough for a person familiar with programing...
I have never taken any programming classes. We are in our 4th week of C.
Well, thanks for the quick reply.
OK, this is what I have so far.... I am lost.
#include <stdio.h>
int main ()
{
int i, count, mask;
mask=0x8000;
i=0x1b53;
printf("Hex value = %d, Binary = %d\n", i, mask);
for (i = 0; i <= 15; i = i + 1)
printf("1");
printf("0");
return 0;
}
OK, more of a hint.
Binary 16 is 10000.
16/16 is 1. Look at the remainder as a binary digit, either 0 or 1. You use the modulus operator for this. The remainder 1 % 2 is 1. The bit is set.
Try it with this:
100000.
This is 32.
32/16 = 2
and 2 % 2 is 0.
Therefore the bit representing 16 is off.
Just apply these rules to your code.
OK, more of a hint.
Binary 16 is 10000.
16/16 is 1. Look at the remainder as a binary digit, either 0 or 1. You use the modulus operator for this. The remainder 1 % 2 is 1. The bit is set.
Try it with this:
100000.
This is 32.
32/16 = 2
and 2 % 2 is 0.
Therefore the bit representing 16 is off.
Just apply these rules to your code.
Thanks for your efforts. However, while you are describing the modulus operator, I am still trying to figure out how the bitwise & is supposed to work out in this thing. I am not a young person, this stuff is quite new to me, I will keep reading and maybe it will make sense.
Appreciate your assistance though.
Your problem description from the first post seems to give very detailed instructions on how to complete the task. Do you know how the bitwise logic operators work?
For instance, what is the result of the following bitwise comparison: - :
-
10110010 11010101
-
& 01001110 11010111
The result is 00000010 11010101. Do you see why this is the result?
How about this one: - :
-
10110101 00101100
-
& 00000000 00000001
The result is 00000000 00000000. Do you see why this is the result?
Finally: - :
-
00011010 11100101
-
& 00000000 00000001
The result is 00000000 00000001. Do you see why this is the result?
The last two examples will really help you to get your problem. Do you see what is happening when you use & with the binary value 1? What if it was 2 (00000000 00000010)? Four? Eight?
Thanks for your efforts. However, while you are describing the modulus operator, I am still trying to figure out how the bitwise & is supposed to work out in this thing. I am not a young person, this stuff is quite new to me, I will keep reading and maybe it will make sense.
1) I'm older than you are.
2) I'll bet I was older than you are now when started C and C++.
3) your original post was to print out the binary value of a 16 bit number
4) you do not use the bitwise AND for that
5) you use the modulus operator (%).
Here's 5, which is binary 101
5/4 = 1 % 2 -> 1
5/2 = 2 % 2 -> 0
5/1 = 5 % 2 -> 1
There are the bits of 5: 101
I think we can all agree that age has nothing to do with programming or this problem. Besides that, it might be nice to keep the remarks to a minimum, I know how it feels to be new and post on forums and getting a response that seems hostile like this one would deter me contributing to a forum... especially since I am new.
Sorry if this is offends you... I enjoy reading these forums and I'd prefer not to see this turn into another 50 post keyboard war.
I think the problem you have is understanding this pseudo-code:
To test for each digit value, bitwise and 'i' with 'mask'
when the result for the bitwise and is true, print the number '1'
when the result for the bitwise and is false, print the number '0'
then shift mask one place to the right
It took me a few reads to understand excactly what he is saying, but what he wants is you to use the '&' operator on i and the mask to get a boolean value... -
bool result = i & mask;
-
if (result)
-
cout << "1";
-
else
-
cout << "0";
-
shifting the mask looks like this:
this moves all the binary digits one place to the right... effectively multiplying everything by 2 or 2^1 more accurately.
Check out wikipedia's explination... you want the Shifts in C++ section and the AND sections: http://en.wikipedia.org/wiki/Bitwise_operation
or something to that extent.
shifting the mask looks like this:
this moves all the binary digits one place to the right... effectively multiplying everything by 2 or 2^1 more accurately.
Actually, shifting to the left does not multiply by 2. Shifting to the left does. Check out this example: - unsigned int i = 53; // 0011 0101
-
i = i >> 1; // Shift all bits one position right
-
// Now i is 0001 1010 (i think), which is 26, or about 53/2
-
-
unsigned int j = 83; // 0101 0011
-
j = j << 1; // Shift all bits one position left
-
// Now j is 1010 0110, which is 166, or 83 * 2
yeah your right. Sorry I don't work with bitwise ops very often.
yeah your right. Sorry I don't work with bitwise ops very often.
It's ok; no one's perfect. Except msquared, and even she segfaults every now and then.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Jaakko Holster |
last post by:
Lets run following code:
------- snip ------
class Perm {
static $read = bindec('001');
static $write = bindec('010');
static $delete = bindec('100');
}
|
by: ai lian |
last post by:
When I use printf to print a large double number, the result is not
the same as the original input number. For example:
double num=899999999999.894400;
printf("%lf\n",num);
The output is:...
|
by: Golan |
last post by:
Hi,
I need to convert a Binary value to Decimal. I've been told that the
value is an unsigned one. How can I do this?
I use memcpy into an unsigned char variable, but when I print the
value I got...
|
by: Michael A. Covington |
last post by:
Is there a way to make a C# program print the date on which it was compiled?
Finding the file date of the executable is one way, but it's not foolproof.
Thanks!
|
by: Steven |
last post by:
Hello,
I want to modify an binary value of stationary location in file. For
example, 100h=0F,
i want to change to 100h=FF, how to do?
Thank you very much
|
by: dmitrey |
last post by:
hi all,
could you inform how to print binary number?
I.e. something like
print '%b' % my_number
it would be nice would it print exactly 8 binary digits (0-1, with
possible start from 0)
...
|
by: castironpi |
last post by:
On May 7, 3:31 pm, Mensanator <mensana...@aol.comwrote:
) for a in range( 10 ) ] )
00000000
00000001
00000010
00000011
00000100
00000101
00000110
|
by: zehra.mb |
last post by:
Hi,
I had written application for storing employee data in binary file and
reading those data from binary file and display it in C language.
But I face some issue with writing data to binary file....
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |