
- Ask a Question

Verilog Hex?
How do you use hex in Verilog?
Please log in or register to answer this question.
Hex in Verilog are 16 based integers. I usually explicitly define the size, then type "'h" and then the value. I use hex values when I need some hardware-close representation of data: register's fields, sometimes in arithmetic, floating point representation, etc. localparam mask = 16'hfc73; reg [15:0] rega = 16'h0e58; assign fields = rega & mask;
Please log in or register to add a comment.
8'h23 = 35 (Decimal) = 0010_0011 (Binary)
© 2022 by Hardware Coder. User contributions are licensed under cc by-sa 4.0 with attribution required . Attribution means a link to the question, answer, user, etc on this site.
This site is owned and operated by Hardware Coder in McKinney, Texas.
Send Us A Message About Us
By using this site, you agree to the following:
Privacy Policy Terms and Conditions DMCA Policy Earnings Disclaimer Legal Disclaimer

- Search forums
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature currently requires accessing the site using the built-in Safari browser.
Welcome to EDAboard.com
Welcome to our site edaboard.com is an international electronics discussion forum focused on eda software, circuits, schematics, books, theory, papers, asic, pld, 8051, dsp, network, rf, analog design, pcb, service manuals... and a whole lot more to participate you need to register. registration is free. click here to register now..
- Digital Design and Embedded Programming
- PLD, SPLD, GAL, CPLD, FPGA Design
how to assign hex value to a variable
- Thread starter j hemangini
- Start date Aug 8, 2008
- Aug 8, 2008
j hemangini
Member level 1.
assign hex value I want to creat a code in which there is variabe name let Data_Byte and want assign hex value 0x41, initially to this variable. Binary equivalent of this value is 01000001. According to my requirement after every 100 clock cycle only one bit should be on out pin (from bit no 0 to 7 one by one after every 100 clock cycle). So the main thing i want to ask is that how can i define this variable and how one bit can be transferred from this value. thank you.
llopacinski2
Newbie level 2.
assign hex values in vhdl If I understood: vhdl: signal Data_Byte : std_logic_vector( 7 downto 0) := x"41"; fifth bit: one_bit <= Data_Byte(5); --(data conversion can be needed) verilog: reg [7:0] Data_Byte = 8'h41; fifth_bit = Data_Byte[ 5];
Member level 2
vhdl assign hex You have to use "signal" not "variable" in order to put it on FPGA pin. first declare it.. signal Data_Byte : std_logic_vector(7 downto 0); hex value assignment.. Data_Byte <= x"BC"; For your problem first initialize.. Data_Byte <= x"01"; then every 100th clock edge, left rotate (by one bit) the whole register content.
vhdl assign value thanks for your reply. now i understood how to define hex value. But what instruction should i write for shift right. Thank you.
assigning hex values to #define Shift operators: Let A = “10010101” A sll 2 = “01010100” --shift left logical, filled with ‘0’ A srl 3 = “00010010” --shift right logical, filled with ‘0’ A sla 3 = “10101111” --shift left arithmetic, filled with right bit A sra 2 = “11100101” --shift right arithmetic, filled with left bit A rol 3 = “10101100” --rotate left by 3 A ror 5 = “10101100” --rotate right by 5 where sll, srl, sla, sra, rol, ror are vhdl keywords. For your application, you have to use: "A rol 1"
shift left logical for hexadecimal your reply was very helpful for me. I m very greatful to u. thank you.
Part and Inventory Search
Welcome to edaboard.com.
- This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register. By continuing to use this site, you are consenting to our use of cookies. Accept Learn more…
Numbers in Verilog
Welcome to my ongoing series covering mathematics and algorithms with FPGAs. This series begins with the basics of Verilog numbers, then considers fixed-point, division, square roots and CORDIC before covering more complex algorithms, such as data compression.
In this first post, we consider integers, dig into the challenges of signed numbers and expressions, and then finish with a bit of arithmetic.
This post was completely revised in November 2022.
Series Outline
- Numbers in Verilog (this post) - introduction to numbers in Verilog
- Vectors and Arrays - working with Verilog vectors and arrays
- Multiplication with DSPs - efficient FPGA multiplication
- Fixed-Point Numbers - precision without complexity
- Division in Verilog - divided we stand
- More maths to follow
Representing Numbers
We’re so familiar with different representations of numbers we hardly give them a second thought.
Some representations of forty-two:
- 101010 (binary)
- 42 (decimal)
- 0x2A (hexadecimal)
- 4.2x10 1 (scientific notation)
- XLII (Roman numerals)
- 四二 (Japanese numerals)
- zweiundvierzig (German)
Different representations express (almost) the same thing but work better (or worse) in different circumstances: hexadecimal is suitable for a memory address, while scientific notation compactly expresses vast and tiny numbers alike.
As a hardware designer, you need to consider how you represent numbers. How many bits do I need? Do I need signed numbers? Will BCD make my design simpler? Is fixed-point accurate enough? What happens when I mix different widths in one expression?
Cistercian Numerals For something a bit less ordinary, try Cistercian numerals (Wikipedia).
Computers famously “think” in binary, and the same is true for most electronics. Off and on, high and low. Simple, right?
For positive integers, things are pretty straightforward.
Let’s take a look at 42 in binary: 101010 2
Each binary digit is twice the previous one: 1, 2, 4, 8, 16, 32…
32 + 8 + 2 = 42
Forty-two requires at least six binary digits to represent in this way.

Binary Coded Decimal
But there are other possible representations: some systems use binary coded decimal (BCD). Packed 1 BCD uses a nibble (4-bit) value to represent each decimal digit.
To get the packed BCD representation, convert each decimal digit into a 4-bit binary value:
The BCD representation requires eight bits to represent 42, two more than the plain old binary version. However, there are advantages, including the ease of display (each nibble is one character) and the ability to accurately represent decimal numbers (such as 10.1).
On typical binary computers, BCD adds overhead for arithmetic operations. However, when designing your own hardware, you can support BCD directly in logic, making it an attractive option for straightforward numerical designs.
Wikipedia’s binary-coded decimal article covers different BCD variants and sign encoding.
Interesting as BCD is, we’ll be sticking with plain old binary for the rest of this document. Many of the concepts we’ll cover also apply to BCD.
Binary in Verilog
By default, a Verilog reg or wire is 1 bit wide. This is a scalar :
A scalar can only hold 0 or 1 (but see Four State Data Types below).
We need a vector to hold values other than 0 and 1.
A vector is declared like this: type [upper:lower] name;
a , b , and c are vectors:
- Wire a handles 0-63 inclusive (2 6 is 64).
- Reg b handles 0-255 inclusive (2 8 is 256).
- Logic c handles 0-4095 inclusive (2 12 is 4096).
You need to ensure your vector is large enough to handle the full range of values your design requires. Synthesis tools are good at discarding unused bits, so it’s better to err on the side of too large rather than too small.
Deciding on the appropriate vector width for an algorithm requires an understanding of that algorithm. For example, I’ve worked with an ellipse drawing algorithm that required 48-bit internal vectors when using 16-bit coordinates.
It’s easy to miss the width from a signal declaration and create a scalar by mistake:
Alas, many tools provide no warning on width mismatches. To catch issues with bit widths, I strongly recommend you lint your designs with Verilator .
Four State Data Types The logic, reg, and wire data types can take one of four values: 0, 1, X, Z , where X is unknown, and Z is high impedance. For this introduction to numbers, we’re going to ignore X and Z .
We cover vectors in detail in the next part of the series: Vectors and Arrays .
Verilog has several options for interpreting literal numbers.
A lone unadorned number, such as 42 is interpreted as a signed 32-bit decimal :
The default interpretation seems reasonable, but sometimes it’s easier to work in another base, and Verilog gives you the option of binary, octal and hexadecimal as well as decimal.
You specify the base (radix) using a single quote followed by a letter:
- d - decimal
- h - hexadecimal
With hexadecimal, you can use the letters a-f and A-F as well as the usual digits:
Unlike the default decimal interpretation, these literals are unsigned , even with a decimal base.
If you want a signed literal with a base, you need to add an s before the base:
You can use a negative sign to create a negative literal. Stick with decimal for literals with minus signs: it quickly gets confusing in other bases.
We’ll be covering signed numbers in detail in the next section.
You specify a literal width in bits by putting it before the single quote:
ProTip: You can include underscores in your literals to improve readability.
What happens if you only specify some of the bits in a literal?
Verilog filled the remaining bits of 8'b1111 with zero, which is what you might expect and is how numbers usually work day-to-day.
It doesn’t matter if the literal is signed or another base; the value is always filled with zeros:
In case you’re wondering about sign extension, fear not we come to that later in this post.
Literal Tricks
In SystemVerilog, you can set all the bits of a vector to ‘1’:
You can also use the concat operator {} to set specific patterns in SystemVerilog and Verilog:
You can nest concat operators, as in the final example above.
Concat allows us to set an appropriate value regardless of the vector width.
Signed Numbers
If your design requires negative values, you need to handle signed numbers. The standard approach is two’s complement , as with almost all CPUs and software.
The Two’s Complement
With two’s complement, addition, subtraction, and multiplication all work as they do with positive binary numbers. But what is the two’s complement? The positive and negative two’s complement representations of an N-bit number add up to 2 N .
For example, with four-bit values: 7 is 0111 and -7 is 1001 because 0111 + 1001 = 10000 (2 4 ).
Discarding the extra bit, the result of adding a number and its two’s complement is always zero.
You can switch the sign of a two’s complement number by inverting the bits and adding one:
You rarely need to determine the two’s complement; Verilog can handle it for you.
Let’s look at a few additions to confirm things work as expected:
To learn more, check out the Wikipedia article on two’s complement .
The range of a two’s complement vector of width n is:
-2 (n-1) to +2 (n-1) -1
For example, an 8-bit signed vector has the range:
-2 7 to +2 7 -1 = -128 to +127
You can’t represent +128 with an 8-bit signed vector.
Try to take the two’s complement of -128:
You get the original -128 back.
Signing Your Signals
Declaring a vector as signed is easy:
For signed literals, as we discussed above, you add the s prefix to the base:
Test Negative
It’s straightforward to check the sign of a number with two’s complement:
- For positive numbers (and zero), the most significant bit is 0
- For negative numbers, the most significant bit is 1
You can check the most significant bit directly:
However, comparison operators are usually clearer:
Signed Expressions
Now we know how to handle signed vectors and literals, it’s time to wrestle with expressions. I’ve done my best to accurately distil a rather dry and complex subject into something palatable, so I hope I don’t offend the language lawyers and bore everyone else.
An expression consists of operands , such as variables and literals, and operators , such as addition and assignment.
Verilog uses the width of the widest operand when evaluating an expression.
It doesn’t matter what the operators are; all Verilog cares about is the width of the operands.
Narrower operands are widened until they’re the same width as the widest. For unsigned operands, Verilog simply fills the new bits with zero, but with signed operands, it uses sign extension .
Sign extension copies the most significant bit (MSB) to fill the width. Remember, for a signed number, the MSB is 1 for negative numbers and 0 otherwise.
This doesn’t sound too bad until you learn that in Verilog:
If all the operands are signed, the result is signed. Otherwise, it’s unsigned.
Verilog doesn’t consider it an error to mix signed and unsigned operands; it treats them all as unsigned. This leads to painful surprises that can be hard to debug.
Take a look at this example:
Running the wider test bench:
Looking at the binary, we can understand where 19 comes from.
When we set m = -4 it has binary value 1100 .
When Verilog evaluates the expression y = u + m :
- m is 4 bits wide, but u and y are 8 bits
- m must be widened to 8 bits to match the widest operands
- u is unsigned, so m is also considered unsigned
- Treated as unsigned, m is widened to 8 bits with zeros: 00001100 (12 in decimal)
- 00001100 + 00000111 = 00010011 (12+7=19 in decimal)
If you take one thing away from this post:
Never mix signed and unsigned variables in one expression!
If the left-hand side of an assignment is smaller than the right-hand side, then the value is truncated. The following examples use literals, but this also applies to expressions and variables.
Because 15 has no base, Verilog treats it as a signed 32-bit decimal:
For a , the truncated bits are all zero, so don’t change the value.
If the right-hand side is signed, truncating it may change its value and sign :
We assigned -13 to e , but after truncation, we get +3.
Your synthesis tool or simulator should warn you about truncated values.
Reckoning with Arithmetic
We’ve talked a lot about the representation of numbers, but we’ve yet to do much maths. Let’s finish this post by having a quick look at the elementary arithmetic operations.
Modern FPGAs include dedicated logic, such as carry chains , to handle addition efficiently: there’s no need to roll your own design. However, there are plenty of university slide decks online if you want to create an adder from scratch.
Before we skip over addition entirely, it’s worth having a quick look at overflow.
Consider the following:
The signals x y z are unsigned and 4 bits wide.
If we set x = 2 and y = 9 then (x + y) = 11 or 1011 2 in binary.
z has the value 1011 2 : z = 11 as expected.
If we set x = 11 and y = 7 , you might expect the result to be (x + y) = 18 or 10010 2 in binary, which is then truncated to fit in z .
However, Verilog uses the width of the widest operand to evaluate expressions. The operands in our case are the three variables x , y , and z , all of which are 4 bits wide. Thus the result of the expression is 0010 2 , and it’s assigned to z .
Modular arithmatic , where the result wraps around, is the norm in hardware and software.
Catching the Overflow
However, there are times when you want to know if overflow has occurred:
The widest operand in our new expression is {c, z} , which is 5 bits wide, so when we evaluate x + y , we get 10010 2 . The value of 1 gets assigned to c while 0010 2 is assigned to z as before.
You could use the carry bit to set an overflow flag when designing a CPU.
Another potential use is with saturation arithmetic : we keep z at its maximum value when overflow occurs:
With this saturation design, if x = 11 and y = 7 , then z = 15 .
Learn more from saturation arithmetic on Wikipedia.
Subtraction
Subtraction is almost the same as addition: subtracting y is equivalent to adding -y . It’s easy to find -y , as we saw earlier when discussing two’s complement .
If we set x = 11 and y = 7 :
Your synthesis tool will handle this, but remember that subtraction is a little more complex than addition. Prefer incrementing over decrementing and register the results of subtractions before using them in subsequent calculations, especially on low-power FPGAs such as the iCE40UP.
Multiplication
Multiplication is more complex than addition or subtraction, but modern FPGAs can handle it with dedicated DSP blocks. Small vectors don’t require too much thought, but the resulting output has potentially twice as many bits as the inputs:
I’ve written a dedicated post on Multiplication with FPGA DSPs that looks at the use of reg and pipelining to improve multiplication performance and minimise logic use.
What about division? I’ve bad news: Verilog won’t synthesise this for you. The good news is that it’s not hard to implement yourself: I have a dedicated post on Division in Verilog that covers integers, fixed-point, and signed numbers.
What’s Next?
In the second part, we look at Vectors and Arrays or jump ahead to Fixed-Point Numbers .
Have a question or suggestion? Contact @WillFlux or join me on Project F Discussions or 1BitSquared Discord . If you like what I do, consider sponsoring me on GitHub. Thank you.
Unpacked BCD uses a whole byte (8-bits) for each decimal digit. ↩︎
Siemens Digital Industries Software

IMAGES
VIDEO
COMMENTS
To convert RGB to Pantone, visit RGB.to, type the hexadecimal value of the RGB in the given field, and click on Convert Hex Color. The following page displays the most identical Pantone color with its name and value. Users can click on the ...
A relative value unit based on a Current Procedural Terminology code assigns a standard work value based on a medical procedure performed by health care providers, according to Advancing the Business of Healthcare. The RVU represents the co...
Baseball cards were first printed in the 1860s, but their first surge in widespread popularity didn’t occur until the turn of the 20th century. PSA, a world-renowned third-party authentication company, assigns a grade of 1 to 10 in determin...
Verilog treats all bare numeric literals as decimal. A and D are not legal decimal values. For hexadecimal literals, you need to specify the
Hex in Verilog are 16 based integers. I usually explicitly define the size, then type "'h" and then the value. I use hex values when I need
//assign the 32 bit value to 12 bits: // d=0000_0110_1010. Page 8. Verilog
You've declared your constants as 8 bit values with the 8' prefix. So 0x63 + 0x63 = 0xc6 which is correct. If you want a 32 bit result then
You have to use "signal" not "variable" in order to put it on FPGA pin. ... signal Data_Byte : std_logic_vector(7 downto 0); hex value assignment.. Data_Byte <= x
This is regardless of the value of cfg. Is there a way to use variables and preferably to specify them in hex? By the way, variables should
Verilog supports binary, octal, decimal, and hexadecimal number formats, with 2's compliment representation for negative numbers. Additionally
// hexadecimal number, 8 bit as xxxx1011. 3′b010 // binary number, 3 bits as
o - octal; d - decimal; h - hexadecimal. // 32-bit wide literals with decimal value 9
Hex values in verilog. how to assign hex value to a variable Forum for Electronics passing .hex file to RTL Verification Academy syntax - How to display
I think, SV does not have any system function to read a file with Mixed data type values.(e.g. int, hex, hex). If you consider implementing file